我是C ++和编程的新手,我需要一些帮助。我不明白为什么当计算机猜到我的号码时它会无限循环。目前我已经把最终功能放在了一边。任何建议将不胜感激。
// Computer guesses number
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); // Generates random number
int myNumber; // My number.
int computersNumber = 0;
int tries = 0;
int highest = 100; // Highest number the computer has put.
int lowest = 1; // Lowest number the computer has put.
cout << "\tHello, in this game the computer will try to find your number.\n\n";
cout << "Please insert your number ( 1 - 100 ): ";
cin >> myNumber; // My number, that the computer is going to guess.
while (myNumber <= 0 || myNumber >= 101) // Checks if user has inputed a legal number.
{
cout << "You entered an illegal number. Please type a new one.\n";
cin >> myNumber; // If user made an illegal choice it asks to input a new one.
}
do
{
int computersNumber = rand() % (highest - lowest) + lowest;
++tries; // adds +1 every time computer guesses wrong number.
if (myNumber > computersNumber)
{
cout << "The computer guessed: " << computersNumber << "\n";
lowest = computersNumber + 1;
}
else if (myNumber < computersNumber)
{
cout << "The computer guessed: " << computersNumber << "\n";
highest = computersNumber - 1;
}
else
{
cout << "Computer guessed your number in " << tries << " tries.\n";
break; // infinite loop if this wasn't here.
}
} while (myNumber != computersNumber);
return 0;
}
答案 0 :(得分:3)
您的问题是您在do-while循环中重新声明computersNumber
。这将创建一个新的本地变量实例,该实例使用do-while中的所有内容,但while()
语句中的条件仍然引用computersNumber
方法中声明的main()
变量因为int computersNumber = rand() % (highest - lowest) + lowest;
告诉C ++在do-while循环中创建一个新变量,而不是修改main
内的原始变量,因此不会被更改。
要解决此问题,请更改此行:
int computersNumber = rand() % (highest - lowest) + lowest;
对此:
computersNumber = rand() % (highest - lowest) + lowest;
答案 1 :(得分:3)
您在do
循环中声明了一个新变量:
int computersNumber = rand() % (highest - lowest) + lowest;
此定义位于循环范围内,在do { }
块之外是不可见的。
您的while (myNumber != computersNumber);
条件正在检查循环外部的int computersNumber = 0;
定义,该定义始终为0.
修复方法是重用循环中的现有变量而不是创建新变量,因此从以下行中删除int
:
int computersNumber = rand() % (highest - lowest) + lowest;
而是使用
computersNumber = rand() % (highest - lowest) + lowest;
。
答案 2 :(得分:3)
您已在循环中声明了另一个名为/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.firebase_icon)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
的变量。 while语句中使用的computersNumber
是您在main函数中声明的另一个变量。
您可以通过在循环中使用相同的变量来解决此问题:
更改此行
computersNumber
并删除int computersNumber = rand() % (highest - lowest) + lowest;
关键字。
int
您可以在以下链接中阅读有关变量范围的更多信息:
https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
答案 3 :(得分:0)
您在两个位置声明computersNumber
。因此,您将一个设置为零int computersNumber = 0;
,而在另一个位置,您将一个随机数分配给int computersNumber = rand() % (highest - lowest) + lowest;
。
设置为零的是您正在检查的那个,因为已设置为随机数的那个仅具有已设置的每次迭代的范围。意味着循环外部{{1} }始终为零,并且在循环内部使用随机值创建新的computersNumber
。因此,只要computersNumber
未设置为零,就会有无限循环。
要解决此问题,只需从myNumber
移除int
,使其成为int computersNumber = rand() % (highest - lowest) + lowest;
,每次循环时,您都会将computersNumber = rand() % (highest - lowest) + lowest;
设置为随机值,如果该随机值匹配computersNumber
它会打破循环。