小编zeb*_*cet的帖子

猜对数字后无法退出while循环

我正在尝试使用 while 循环制作一个简单的数字猜测程序,但即使在找到正确的数字后(我将 num 更改为 5 并且它不起作用),它也不会退出 while 循环。你能告诉我问题出在哪里吗?

int guess;
int num;

cout << "Enter the number (0-10): ";
cin >> guess;

while(guess != num){
    int num = rand()%10;
    cout << "Nope, keep trying." << endl;
    cout << "Enter the number (0-10): ";
    cin >> guess;
}

cout << "Congrats, you've found the number!" << endl;
Run Code Online (Sandbox Code Playgroud)

c++ random

1
推荐指数
1
解决办法
72
查看次数

标签 统计

c++ ×1

random ×1