我正在尝试使用 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)