- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在第一门 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;
}
最佳答案
这里有一些东西......
将 srand(time(0));
移出 while 循环并移至 main
的顶部。如果您在同一秒内重复播种(如果 time(0)
不变),您将两次获得相同的“随机”数字。
操作
,num3
会怎样?你永远不会初始化 num3
,所以如果他们没有选择有效的 operation
,num3
就会有一个垃圾值。然后继续运行一个循环,其条件取决于 num3
的值! (while (guess != num3)
)
else { ... if {
与 else if {
在最后一个循环中,将 if (play == Y)
和 else if (play == Q)
带出嵌套的 if
并使它们成为 else if
。
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/
关闭。这个问题是off-topic .它目前不接受答案。 想要改进这个问题? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正在在线开发哈佛 CS50 的 PSET3,并且在 CS50 十五场游戏的“获胜”功能方面遇到问题。 该函数适用于 4x4 网格拼图,但不适用于 3x3 对于 3x3 的情况,该函数返回 true
我在 pset 的最后一部分,每当我使用 ./fifteen 3 #include #include #include // b
我正在学习 Javascript 并开始了第一个游戏元素:井字游戏。 我的元素及其代码: Tic Tac Toe Project 如果用户在井字游戏中获胜,我希望能够显示通知。 现在我只想测试我的“获
我刚刚将 MAMP 3 升级到 MAMP 4,而 MySQL 不再启动。 错误日志文件一遍又一遍地写着: 160905 11:22:17 mysqld_safe mysqld restarted 20
我目前正在构建一款非常流行的 Tic-Tac-Toe 游戏的复制品,该游戏使用纯 ruby 构建并经过测试的 TDD 风格。 我遇到的唯一问题是,在我的一生中,我无法弄清楚如何检查玩家何时获胜。我知道
我想构建 OSX 小部件以快速制作基于流数据的可视化仪表板组件的原型(prototype)。我想使用 d3.js 来进行显示,但是我尝试的最基本的事情都遇到了错误,我没有能力/耐心去解析。 在我开始在
我正在尝试在 android 中制作谷歌地图应用程序。我在 SDK 管理器中安装了 Google Play 服务。当我运行该应用程序时,模拟器中出现错误文本: App won't run unless
我的模拟器有问题,它不想启动,它阻止了加载屏幕。 最佳答案 我也遇到了这个问题,解决方法是打开模拟器,然后在顶部菜单中:Simulator > Reset content and setting。 关
我知道我需要一个 target="_blank,但它在我放置它的任何地方都不起作用。请帮忙! 最佳答案 你可以试试这个... 关于javascript - 添加目标 ="_blank to my
我正在用 PHP 使用 mySQL 编写一个模拟选举的网络应用程序。 我有三个表:Candidates、Elections 和 Votes。 Votes 包含 CandidateID、Election
在David Beazley's talk on generators ,他说,作为一个警告: Functions that consume an entire iterable won't term
我正在通过 Android Studio 使用 Android 模拟器来实现一个使用 Google map 的应用程序,但是当我运行该应用程序时,我在模拟器屏幕上收到以下消息: “如果您的设备不支持
我正在 Salesforce 中创建 DocuSign 自定义按钮。在运行 DocuSign 按钮逻辑之前尝试在某些字段值不满足时添加错误。 我想要的条件是,如果机会阶段的选择列表值不等于“已关闭”,
我正在尝试使用 BaseGameUtils 将游戏集成到我的应用程序中。一切看起来都很好,但是当我运行该应用程序时,我收到消息“在您更新 Google Play 服务之前,此应用程序不会运行”,当我单
我正在开发的网站上使用 jplayer html5 媒体播放器,但它无法在 Chrome 上播放某些 mp3。我试过在浏览器的 native 播放器中播放这些 mp3,但两者都无法播放。另一方面,当我
在 Programming Rust 一书中的 References Are Never 部分,他们指出没有与 C 的 NULL 类似的东西。那一点我明白了。然后他们说 Rust won't conv
Auto-Scaling 下的 EC2 可能由于内存问题而无法扩展。我的自动缩放设置会检查 CPU,因为默认情况下无法将内存添加到 cloudwatch 指标中。有什么建议可以让我的服务器在上述情况下
每当我启动 Azure Cloud Shell 时,都会收到此错误: 无法装载 azure 文件共享。您的云驱动器将不可用您的 Cloud Shell session 将是短暂的,因此任何文件或系统更
我在 UITableViewCell 中有一个灵活的可编辑 UITextView。一个简单项目的完整源代码可以在 https://github.com/AlexChekanov/TextViewInT
我是一名优秀的程序员,十分优秀!