简体   繁体   English

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

Today we have assignment on making random number game. 今天,我们要分配随机数字游戏。 teacher tells tells us we only can use what we learn so far. 老师告诉我们,我们只能使用到目前为止所学到的东西。 we did not learn any loop, so i'm stuck using only if/else. 我们没有学习任何循环,所以我只能使用if / else。 how do i use only if/else statement to make random number game loop 8 time and at end of 8 turn it show answer? 我如何仅使用if / else语句使随机数游戏循环8次并在8结束时显示答案?

i start this code but clueless on how to continue. 我开始这段代码,但对如何继续一无所知。

int main(){
   int Guess;
   cout << "Guess my number!\n\n";
   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;

}

如果/线性非常线性,我不认为您真的可以回到使用它们的开始,也许您的老师表示您可能必须重复相同的行8次,这似乎有点多,但是如果您将代码更简单,这可能很简单,您是否必须告诉玩家他们的猜想是更大还是更小,也许您可​​以给他们8次尝试,让他们猜测1到20之间的数字,我认为没有while / for循环这样做不是一个好习惯,如果您仅使用循环,它将使一切变得更简单。

I don't think there's any way to loop back using an if-else block. 我认为没有任何方法可以使用if-else块进行循环。 I'm guessing your teacher is giving you this exercise so that you can appreciate loops. 我猜你的老师正在给你这个练习,这样你就可以欣赏循环了。 Anyway, here's a somewhat neat solution using switch case. 无论如何,这是一个使用开关盒的简洁解决方案。

int main() {
    int Guess;
    cout << "Guess my number!\n\n";
    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;
}

Since I don't really know what you covered in class, I'll just make a list of the options. 由于我真的不知道您在课堂上所学的内容,因此只列出了一些选项。 There are many kinds of "loops": 有许多种“循环”:

  • goto loops, using labels. 使用标签goto循环。
  • for loops for循环
  • while loops while循环
  • do {} until loops do {} until循环
  • Unrolled loops ( aka. Copy-Paste) 展开的循环(又名复制粘贴)
  • Recursion 递归
    • You can also use recursion by calling your executable from within the executable 您还可以通过从可执行文件内部调用可执行文件来使用递归
    • You could try calling main() from inside main() and use arguments, or a global or static variable as counter. 你可以尝试调用main()从内main()和使用参数,或者全局或static的计数器变量。

Keep in mind there's also the chance that there was something quickly covered in class which you missed, and which isn't in chapters 1-4. 请记住,也有可能您错过了课堂上快速涵盖的内容,而这些内容不在第1-4章中。 You said: Covered in class or in the book. 您说:在课堂上书中都有涉及。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++ 中创建一个循环一定次数的循环? - How to create a loop in C++ that loops a certain amount of times? C ++ while循环,使用if else语句在数组中计数 - C++ while loop, using if else statements to count in an array 如何限制while循环运行的次数? C ++ - How to limit the amount of times a while loop runs? C++ 我怎么能把这个硬代码变成 C++ 中的 while 循环? - How could I make this hard code into a while loop in C++? C ++-如何制作一个简单的计时器,以及如何基于它使用if语句? - C++ - How to make a simple timer and how would I go about using if statements based on it? 如何使用C ++中的switch语句进行循环? - How do you make a loop with switch statements in c++? 如何检查C ++ std :: vector等中的成员身份? - How do I check for membership in a C++ std::vector, etc.? 我没有使用string.h编写了C ++ Palindrome。 它在没有Do-While循环的情况下工作正常。 但是当我尝试进行循环时,它会多次打印结果 - I wrote a C++ Palindrome without using string.h. It works fine without the Do-While loop. But as i tried make a loop it prints the result many times 在 C++ 中的同一个数组中工作时,如何将 int 随机化一定次数? - How to randomise an int a certain amount of times while working in the same array in c++? 在 switch 语句相同但对于某些替换变量/向量/等的情况下,如何避免重复代码? - How do I avoid repetitive code in the case of switch statements which are the same but for some substituted variables/vectors/etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM