gpt4 book ai didi

c++ - If/else 循环 : C++ Program: Won't display final prompt/final loop

转载 作者:行者123 更新时间:2023-11-30 01:37:02 28 4
gpt4 key购买 nike

我在第一门 C++ 类(class)的作业中遇到了很多麻烦。

我已经想出如何让它正确地询问用户他们想使用什么操作(加、减、乘),生成 0-9 之间的随机数,以及如何要求用户解决问题和回答是对还是错。

在此之后,程序应该询问用户是想继续(按 y)还是退出(按 Q),并在用户输入任何其他字母时向用户显示错误消息,但对于由于某些原因,这部分在运行程序时没有显示。

如何让循环正常工作,允许我执行最后的提示,然后仅在按 Y 时重复整个程序或在按 Q 时退出?

注意:总的来说,我对编码还很陌生,这是我的第一门 C++ 类(class),所以我还不知道如何让这段代码更简洁:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()

{
while (true)
{
// Generate two random single-digit integers btwn 0-9
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
int operation, play, num3, guess, Y, Q;

// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}

cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;

if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
}

else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;

if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
}

else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}

else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;

if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
}

else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}

else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;

if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
}

else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}

while (guess != num3)
{
int play, Y, Q;
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cin >> play;

if (play != Y || play != Q)
{
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cin >> play;
}

else
{
if (play == Y)
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}


else if (play == Q)
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
break;
}
}
}
return 0;
}

最佳答案

这里有一些东西......

1。只播种一次

srand(time(0)); 移出 while 循环并移至 main 的顶部。如果您在同一秒内重复播种(如果 time(0) 不变),您将两次获得相同的“随机”数字。

2。如果他们没有输入有效的操作num3 会怎样?

你永远不会初始化 num3,所以如果他们没有选择有效的 operationnum3 就会有一个垃圾值。然后继续运行一个循环,其条件取决于 num3 的值! (while (guess != num3))

3。 else { ... if { else if {

相同

在最后一个循环中,将 if (play == Y)else if (play == Q) 带出嵌套的 if 并使它们成为 else if

4。你最后的循环条件不正确

while (guess != num3) 真的对吗?你想循环直到他们输入有效输入,那么你为什么要循环 while guess != num3

关于c++ - If/else 循环 : C++ Program: Won't display final prompt/final loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50518904/

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