初学者编码挑战的麻烦
Trouble with a beginner coding challenge
编写一个程序,持续要求用户输入 5 以外的任何数字,直到用户输入数字 5。
然后告诉用户"Hey! you weren't supposed to enter 5!"并退出程序。
★ 修改程序,迭代10次后,如果用户还没有输入5,则告诉用户"Wow, you're more patient then I am, you win."并退出。
★★ 修改程序,使其要求用户输入任何数字,而不是与他们被要求输入数字的次数相等的数字。 (即在第一次迭代 "Please enter any number other than 0" 和第二次迭代 "Please enter any number other than 1"m 等。当用户输入他们被要求不要输入的数字时,程序必须相应地退出。)
我的大部分程序都可以运行。我已经达到了这样的程度,它要求一个从 0 开始并上升的数字,它在 10 次尝试后向用户提供耐心的消息,如果他们输入了他们不应该输入的数字,则退出程序。但是,如果用户输入的数字高于它告诉您不要输入的数字,则程序退出时不会显示任何消息。
我真的不知道要搜索什么来解决这个问题。然而,我已经尝试移动一些东西,并摆脱了一些冗余变量。
任何提示将不胜感激,请不要预先给我答案!这是我目前所拥有的。
#include <iostream>
int main()
{
const int GUESS = 1; // constant for number of tries
const int PATIENCE = 10; // constant for message at 10 tries
int UserNum; // player input
int InputNum = GUESS; // intializes GuessNumber
// asks for player input
do
{
std::cout << "Enter any number other then "<< InputNum << ": ";
std::cin >> UserNum;
// exits program if user inputs the number displayed
if (UserNum == InputNum)
{
std::cout << "Hey! you weren't supposed to enter " << InputNum << "!\n";
}
// increase the Guess counter if they dont enter the number displayed
else if (UserNum != InputNum)
{
InputNum++;
}
if (InputNum == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
} while (UserNum != InputNum);
return 0;
}
你的问题在do while
循环条件
首先执行语句,然后检查条件
例如
InputNum
初始化为1
所以如果您输入 2
作为 UserNum 的输入,在 else if
条件下,InputNum
将增加到 2
评估此条件时
while (UserNum != InputNum)
它将是错误的,因为 2==2
循环中断
解决方案
更改 PATIENCE = 11
并使用
while (1)
// this will run infinitely but it will break after 10 iteration or when u press the same number which u shouldn't
而不是
while (UserNum != InputNum)
完整程序
#include <iostream>
int main()
{
const int GUESS = 1; // constant for number of tries
const int PATIENCE = 11; // constant for message at 10 tries
int UserNum; // player input
int InputNum = GUESS; // intializes GuessNumber
// asks for player input
do
{
std::cout << "Enter any number other then " << InputNum << ": ";
std::cin >> UserNum;
// exits program if user inputs the number displayed
if (UserNum == InputNum)
{
std::cout << "Hey! you weren't supposed to enter " << InputNum << "!\n";
break;
}
// increase the Guess counter if they dont enter the number displayed
else if (UserNum != InputNum)
{
InputNum++;
}
if (InputNum == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
} while (1);
system("pause");
return 0;
}
嘿,试试这个程序,它完全符合您的要求。
#include <iostream>
int main ()
{
int GUESS = -1; //loop variable
const int PATIENCE = 10; // constant for message at 10 tries
int InputNum; // input from user
std::cout << "PATIENCE Test" << "!\n";
do
{
GUESS++;
// asks for player's input
std::cout << "Enter any number other than " << GUESS << ": ";
std::cin >> InputNum;
// exits program if user inputs the number displayed
if (GUESS == InputNum)
{
std::
cout << "Hey! you weren't supposed to enter " << GUESS << "!\n";
break;
}
if (GUESS == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
}
while (GUESS != InputNum);
return 0;
}
编写一个程序,持续要求用户输入 5 以外的任何数字,直到用户输入数字 5。 然后告诉用户"Hey! you weren't supposed to enter 5!"并退出程序。
★ 修改程序,迭代10次后,如果用户还没有输入5,则告诉用户"Wow, you're more patient then I am, you win."并退出。
★★ 修改程序,使其要求用户输入任何数字,而不是与他们被要求输入数字的次数相等的数字。 (即在第一次迭代 "Please enter any number other than 0" 和第二次迭代 "Please enter any number other than 1"m 等。当用户输入他们被要求不要输入的数字时,程序必须相应地退出。)
我的大部分程序都可以运行。我已经达到了这样的程度,它要求一个从 0 开始并上升的数字,它在 10 次尝试后向用户提供耐心的消息,如果他们输入了他们不应该输入的数字,则退出程序。但是,如果用户输入的数字高于它告诉您不要输入的数字,则程序退出时不会显示任何消息。
我真的不知道要搜索什么来解决这个问题。然而,我已经尝试移动一些东西,并摆脱了一些冗余变量。
任何提示将不胜感激,请不要预先给我答案!这是我目前所拥有的。
#include <iostream>
int main()
{
const int GUESS = 1; // constant for number of tries
const int PATIENCE = 10; // constant for message at 10 tries
int UserNum; // player input
int InputNum = GUESS; // intializes GuessNumber
// asks for player input
do
{
std::cout << "Enter any number other then "<< InputNum << ": ";
std::cin >> UserNum;
// exits program if user inputs the number displayed
if (UserNum == InputNum)
{
std::cout << "Hey! you weren't supposed to enter " << InputNum << "!\n";
}
// increase the Guess counter if they dont enter the number displayed
else if (UserNum != InputNum)
{
InputNum++;
}
if (InputNum == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
} while (UserNum != InputNum);
return 0;
}
你的问题在do while
循环条件
首先执行语句,然后检查条件
例如
InputNum
初始化为1
所以如果您输入 2
作为 UserNum 的输入,在 else if
条件下,InputNum
将增加到 2
评估此条件时
while (UserNum != InputNum)
它将是错误的,因为 2==2
循环中断
解决方案
更改 PATIENCE = 11
并使用
while (1)
// this will run infinitely but it will break after 10 iteration or when u press the same number which u shouldn't
而不是
while (UserNum != InputNum)
完整程序
#include <iostream>
int main()
{
const int GUESS = 1; // constant for number of tries
const int PATIENCE = 11; // constant for message at 10 tries
int UserNum; // player input
int InputNum = GUESS; // intializes GuessNumber
// asks for player input
do
{
std::cout << "Enter any number other then " << InputNum << ": ";
std::cin >> UserNum;
// exits program if user inputs the number displayed
if (UserNum == InputNum)
{
std::cout << "Hey! you weren't supposed to enter " << InputNum << "!\n";
break;
}
// increase the Guess counter if they dont enter the number displayed
else if (UserNum != InputNum)
{
InputNum++;
}
if (InputNum == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
} while (1);
system("pause");
return 0;
}
嘿,试试这个程序,它完全符合您的要求。
#include <iostream>
int main ()
{
int GUESS = -1; //loop variable
const int PATIENCE = 10; // constant for message at 10 tries
int InputNum; // input from user
std::cout << "PATIENCE Test" << "!\n";
do
{
GUESS++;
// asks for player's input
std::cout << "Enter any number other than " << GUESS << ": ";
std::cin >> InputNum;
// exits program if user inputs the number displayed
if (GUESS == InputNum)
{
std::
cout << "Hey! you weren't supposed to enter " << GUESS << "!\n";
break;
}
if (GUESS == PATIENCE)
{
std::cout << "Wow, you're more patient then I am, you win.\n";
break;
}
}
while (GUESS != InputNum);
return 0;
}