【问题标题】:How do I loop a variable?如何循环变量?
【发布时间】:2014-03-04 17:10:44
【问题描述】:

我已经制作了一个“程序”,只是说欢迎!键入要彼此相加的两个数字: 在那里你输入两个数字然后你得到答案...... 完成后,它会说: Press any key to continue 。 . . 当您按下某个键时,程序会关闭,但我希望它在您按下任何键时重新启动... 我怎么做?我将 Microsoft Visual Studio Express 2013 用于 Windows 桌面... 语言是 C++

这是我的代码:

 #include <iostream>
   #include <limits>
   #include <cstdio>
   using namespace std;

   int Add(int x, int y)
   {


        cout << "Calculating the sum of " << x << " + " << y << "\n";
        return (x + y);
    }

     int main()
     {
         cout << " Welcome!\n";
         int a, b, c;
         cout << "Type two numbers you want to be added to each other: ";
         cin >> a;
         cin >> b;
         c = Add(a, b);
         cout << "The answere is: " << c;
         cout << "\nShutting down....\n\n";
         system("pause");
         return 0;
    }

【问题讨论】:

标签: c++


【解决方案1】:

要循环,你可以使用while

例如:

while (false) {
    std::cout << "You will never see this output" << std::endl;
}

bool loop = true;
while (loop) {
    std::cout << "Type 'quit' to quit this loop." << std::endl;
    std::string input;

    // This will grab a *single word* from the input. If you want a line, look
    // at std::getline
    std::cin >> input;
    if (input == "quit") {
        loop = false;
    }
}

while (true) {
    std::cout << "This will be repeated forever" << std::endl;
}

还有另外两种形式,do while

std::string input;
do {
    std::cout << "Type 'quit' to quit." << std::endl;
    std::cin >> input;
} while (input != "quit");

...和for(通常用于循环定义的事物列表):

for (size_t i = 0; i < 10; ++i) {
    std::cout << i << " out of 10" << std::endl;
}

从技术上讲,您可以将这些循环类型中的任何一种用于任何类型的循环,但我怀疑您想要的类型是两个标准无限循环之一(无论您喜欢哪个):

while (true) {
    // stuff to repeat forever
}

for (;;) {
    // stuff to repeat forever
}

... 或类似于上述do { ... } while (input != "quit"); 循环的do while 循环。

【讨论】:

  • 你也可以使用fordo-while
  • @Jefffrey 我只是在输入那个:D
【解决方案2】:
     int main()
     {
         cout << " Welcome!\n";
         int a, b, c;
         while (true)
         {
             cout << "Type two numbers you want to be added to each other: ";
             cin >> a;
             cin >> b;
             c = Add(a, b);
             cout << "The answere is: " << c;
             cout << "\nPress a key to go again....\n\n";
             system("pause");
         };
         return 0;
    }

【讨论】:

    【解决方案3】:

    你可以这样做:

         int main()
         {
             cout << " Welcome!\n";
             int a, b, c;
    
             while(true) {
                 cout << "Type two numbers you want to be added to each other: ";
                 cin >> a;
                 cin >> b;
                 c = Add(a, b);
                 cout << "The answere is: " << c;
                 cout << "\nPress any key to continue\n";
                 system("pause");
             }
             return 0;
        }
    

    【讨论】:

      【解决方案4】:

      使用 do while 循环提及您的情况 在退出程序之前,以便您可以确定何时继续以及何时退出

      【讨论】:

      • 这应该是一条评论!
      • @πάνταῥεῖ 这与您的纯代码答案有何不同?
      • @πάνταῥεῖ 到目前为止,这个答案实际上比你自己的更具描述性。
      • @πάνταῥεῖ 的回答为这个问题提供了一个实际的解决方案。它缺少任何额外的上下文仅仅意味着它不太可能获得支持。
      • @navTa:我刚才提到,当我们使用 do while 时,而不是使用 while(true),我们可以确定何时退出代码。而当我们再次使用 while(true) 时,我们需要设置一个条件才能正常退出代码
      【解决方案5】:

      我不确定我是否理解您的问题,但我认为这就是您要寻找的。有一个布尔值来确定程序是否会循环。

      int main() {
          // stillRun is true while we want to keep looping the program 
          boolean stillRun = true; 
      
          while(stillRun) { 
              runProgram() ; // this function has all the other code in your old main() function
              cin >> stillRun ; 
          }   
      }   
      

      【讨论】:

        猜你喜欢
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 2020-03-24
        • 2011-12-18
        • 1970-01-01
        相关资源
        最近更新 更多