gpt4 book ai didi

c++ - 如何在同一个 while 循环中评估大于和小于的数字?

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:15 24 4
gpt4 key购买 nike

// DiceRollProject.cpp : Defines the entry point for the console     application.
//

#include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;

int diceRoll(int max); // function definition
int getValidInteger();// function definition

int main() {

srand(time(0)); // seed the random number generator

int exitProgram = 0;
int guess, rollValue;
int maxRollValue = 6;
cout << "Hello! Let's play a dice game. Let me do the first roll for you.\n" << endl;
rollValue = diceRoll(maxRollValue);
cout << "In this roll, you got: " << rollValue << "\n" << endl;

do {
rollValue = diceRoll(maxRollValue);


cout << "What's your guess for the next roll? Enter an integer between 1 and " << maxRollValue << ": ";
guess = getValidInteger();
// TODO: Validate input

if (guess > rollValue)
{
cout << "The guess was too high!";

}

if (guess < rollValue)
{
cout << "The guess was too low!";

}

if (guess == rollValue)
{
cout << "You guessed correctly, congrats!";

}

cout << "In this roll, you got: " << rollValue << "\n" << endl;
// TODO: Evaluate result


cout << "Enter 1 to exit or any other integer to continue rolling ";
exitProgram = getValidInteger();
cout << "\n";
if (exitProgram == 1)
{
cout << "Sorry to see you go. Have a wonderful day!\n" << endl;
}

} while (exitProgram != 1);

return 0;
}

// Roll the die
int diceRoll(int max) {
int rollValue;

rollValue = (rand() % max) + 1;

return rollValue;
}


// Check if user entered an integer
int getValidInteger() {
int userInput;

cin >> userInput;

while (userInput < 1) {

if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1\n";
}

if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6\n";
}

}


if (cin.fail()) {
cin.clear();
cin.ignore();
cout << "Please enter an Integer only ";
cin >> userInput;
cout << "\n";
}

return userInput;
}

我有一个掷骰子猜谜游戏,我正在尝试评估用户输入,以确保他们不能输入小于 1 和大于 6 的数字,不幸的是,仅使用我的 if 语句,他们可以仍然输入这些数字,虽然显示输入无效的字符串,但我想做一个 while 循环,不断要求他们输入等于或大于 1 且等于和小于 6 的有效数字,如果用户保持输入不正确的数字,while 循环将不断询问他们输入有效数字,直到他们输入一个数字,然后程序将正常运行。

最佳答案

首先,在 while 循环中有死代码。

while (userInput < 1)  {

if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1\n";
}

if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6\n";
}

}

在循环体内,第一个 if 始终为真,第二个始终为假。当用户写入无效输入时,您应该进入循环。这发生在 (userInput < 1 or userInput > 6)

在 while 的条件求值后,你应该要求用户写输入

do  {
cout << "Please enter an Integer only ";
cin >> userInput;
if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1\n";
}

if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6\n";
}

}while(userInput < 1 || userInput > 6);

关于c++ - 如何在同一个 while 循环中评估大于和小于的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39907372/

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