如何仅使用if-else语句(no-for、do/while等)使此代码循环一定次数?(c++)
how would i make this code loop a certain amount of times only using if else statements (no for, do/while,etc.)? (c++)
今天我们有关于制作随机数游戏的作业。老师告诉告诉我们只能使用我们目前所学的东西。我们没有学习任何循环,所以我一直在使用onlyif/else。如何使用if/else语句使随机数游戏循环8次,并在8回合结束时显示答案?
我启动了这个代码,但不知道如何继续。
int main(){
int Guess;
cout << "Guess my number!nn";
cout << "Enter a number between 1 and 100. n";
cin >> Guess;
unsigned Number = time(0);
const int Min = 1, Max = 100;
srand(Number);
Number = (rand() % (Max - Min + 1)) + Min;
int tries = 8;
if (Guess < Number && tries > 0){
tries -= 1;
cout << "guess is lower than the number! try again. tries:" << tries << endl;
cin >> Number;
}
else if (Guess > Number&& tries>0){
tries -= 1;
cout << "guess is higher than the number! try again. tries:" << tries << endl;
cin >> Number;
}
else{
cout << "your answer is right the number is:" << Number << endl;
}
if (tries == 0){
cout << "number is: " << Number << endl;
system("pause");
return 0;
}
system("pause");
return 0;
}
如果/else是线性的,我认为你真的不能回到最初使用它们,也许你的老师的意思是你可能必须重复相同的行8次,这可能看起来有点多,但如果你让代码更简单一点,它可能会非常直接,你必须让它告诉玩家他们的猜测是大是小吗,也许你可以给他们8次尝试,让他们猜出一个介于1到20之间的数字,我认为如果没有while/for循环,这不是一个好的练习,如果你只使用循环,一切都会变得更简单。
我认为没有任何方法可以使用if-else
块进行循环。我猜你的老师给你做这个练习是为了让你能欣赏循环。无论如何,这里有一个使用switchcase的简洁的解决方案。
int main() {
int Guess;
cout << "Guess my number!nn";
cout << "Enter a number between 1 and 100. n";
cin >> Guess;
unsigned Number = time(0);
const int Min = 1, Max = 100;
srand(Number);
Number = (rand() % (Max - Min + 1)) + Min;
int tries = 8;
switch (tries) {
case 8:
if (Guess == Number) {
cout << "your answer is right the number is:" << Number << endl;
break;
} else if (Guess < Number) {
cout << "guess is lower than the number! try again. tries:" << tries << endl;
} else {
cout << "guess is higher than the number! try again. tries:" << tries << endl;
}
cin >> Guess;
tries--;
case 7:
if (Guess == Number) {
cout << "your answer is right the number is:" << Number << endl;
break;
} else if (Guess < Number) {
cout << "guess is lower than the number! try again. tries:" << tries << endl;
} else {
cout << "guess is higher than the number! try again. tries:" << tries << endl;
}
cin >> Guess;
tries--;
// repeat code
case 1:
if (Guess == Number) {
cout << "your answer is right the number is:" << Number << endl;
break;
} else if (Guess < Number) {
cout << "guess is lower than the number! try again. tries:" << tries << endl;
} else {
cout << "guess is higher than the number! try again. tries:" << tries << endl;
}
cin >> Guess;
tries--;
default:
if (Guess == Number) {
cout << "your answer is right the number is:" << Number << endl;
} else {
cout << "number is: " << Number << endl;
}
}
system("pause");
return 0;
}
由于我真的不知道你在课堂上涵盖了什么,所以我只列出一个选项列表。有很多种"循环":
goto
循环,使用标签for
回路while
回路do {} until
回路- 展开的循环(也称为复制粘贴)
- 递归
- 您还可以通过从可执行文件中调用可执行文件来使用递归
- 您可以尝试从
main()
内部调用main()
,并使用参数、全局变量或static
变量作为计数器
请记住,课堂上也有可能很快就涵盖了一些你错过的内容,而这些内容不在第1-4章中。你说:在课堂上被覆盖或在书中被覆盖。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在这个代码结束循环中没有得到结束
- 下面是我为检测链接列表中的循环而制作的代码
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 在java中解决这段代码时面临循环中的问题
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 我的代码运行良好,但在游戏循环中中断
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 如何编译使用循环引用的代码?
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 使用基于数组和范围的 For 循环替换一些基本代码行
- 为什么这段代码会导致无限循环?
- 如果代码包含多个复杂度循环,如何计算复杂度
- 无休止的 do while 循环C++代码
- 循环代码错误C++
- C++ while 循环代码的含义
- 如何将查找组合的几个循环代码转换为递归方法
- 如何使用MySQL将c++for循环代码加速到查询中
- 如何将一系列if/else-if/else-if/关系链转换为线性循环代码
- 当我所做的只是将循环代码移动到函数中时,代码运行速度会慢10倍