gpt4 book ai didi

c++ - jar 里有多少口香糖。 2个错误

转载 作者:行者123 更新时间:2023-11-27 23:33:22 26 4
gpt4 key购买 nike

尝试编写代码来猜测口香糖 jar 里有多少口香糖,有点像。我不断遇到两个问题:

  1. 它应该说猜测是高还是低,但只有当我输入超过 1000 的数字时才会出现这种情况,它会说:

Enter your guess: 1001
Too High!

Too Low!

Enter your guess:

  1. 如果您输入一个字母或短语,它会出现乱码:

Enter your guess:
Too High!

Too Low!

但它一直说每分钟大约几百次甚至一千次 -.-

#include<iostream>
#include<ctime>
using namespace std;

int main(void)
{
int iGumballs;
int iUserguess;
int iGuesses = 0;

while(true)
{
system("CLS");
cin.clear();
iGuesses = 0;

srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the gumball jar, you guess!" << endl;
do
{
cout << "Enter your guess: ";
cin>> iUserguess;
if(iUserguess > iGumballs)
{
cout << "Too High!" << endl << endl;
}
if(iUserguess > iGumballs)
{
cout << "Too Low!" << endl << endl;
}
iGuesses ++;
}while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amount of gumballs! High Five!" << endl << endl;
cout << "You took" << iGuesses << " guesses" << endl << endl;
system("PAUSE");
}
return 0;
}

最佳答案

让我提示一个问题。让我们看一下打印消息“too high”和“too low”的两个检查:

    if(iUserguess > iGumballs)
if(iUserguess > iGumballs)

当我把它们放在一起时,有什么不对吗?

关于c++ - jar 里有多少口香糖。 2个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com