如何仅使用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++)

本文关键字:循环 代码 c++ 语句 if-else 何仅使 no-for while do      更新时间:2023-10-16

今天我们有关于制作随机数游戏的作业。老师告诉告诉我们只能使用我们目前所学的东西。我们没有学习任何循环,所以我一直在使用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章中。你说:在课堂上被覆盖或在书中被覆盖