【问题标题】:Repeat a string N times with while loop in C++在 C++ 中使用 while 循环重复字符串 N 次
【发布时间】:2017-10-06 08:00:10
【问题描述】:

我刚刚开始使用 C++,但在使用 while 循环时遇到了困难。

以下是说明: 更改经典的 Hello World 程序: 以便程序打印N 乘以“HelloWorld”字符串(在单独的行上), N 由用户输入。 提示:

  • 使用cin >>获取N
  • 使用带有计数器变量的 WHILE 循环来处理重复。
  • 小心使用cout <<"\ n"endl 以实现反弹。

这是我的代码;我不知道在 while 循环中放入什么来打印 Hello World 字符串 N 次。

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  // Variabili 
  int n;
  cout<<"Inserisci il numero di volte che vuoi ripetere la stringa Hello World!:";
  cin>>n;
  cout<<"Hai deciso di ripetere la stringa"<<" "<<n<<" "<<"volte";

  // Ciclo While che ripete la stringa n volte
  while()
  {
    cout << "Hello World!" << endl;
  }
  system("PAUSE");  
  return 0;
}

【问题讨论】:

  • while(n &gt; 0) { cout &lt;&lt; "Hello World!" &lt;&lt; endl; n--; }
  • 您知道什么,一般来说,即抽象地,应该进入(){}while 吗?我建议在使用@GauravPathak 评论中的纯代码答案之前尝试理解这一点。
  • @Yunnosch 是的,我知道它们应该包含什么,例如 () 中的条件,然后是 {} 中的操作,但我不知道如何根据指令调整它们。
  • 我真的不明白你的意思“在指令的基础上”。
  • 到现在为止,你有没有在你的学习中看到任何工作的while(...){...}?您是否完成了有关循环的教程?

标签: c++ string


【解决方案1】:

用下面的方式想一想:

while(condition){
  BODY;
} 

继续执行 BODY 直到 condition 为真。
例如,下雨时,您通常会在下雨时打开雨伞。当条件正在下雨不再是真的时,你把伞收起来,对吧?

    while(isRaining){
       holdUmbrella();
    } 
    closeUmbrella();

你的情况的条件是,继续写,直到我写的“Hello World”少于N。所以这个想法是计算你打印的次数。为此,您可以使用每次打印时递增的计数器。 while 的条件检查计数器不大于N。像下面这样的东西应该可以工作。

int counter=0;
while(counter<N)
{
    cout << "Hello World!" << endl;
   //remember that you printed one time more. increment counter
    counter=counter+1;
}

【讨论】:

  • 我喜欢你的回答方式。但我认为伞式示例比有用更令人困惑。我会更像while(raining){} CloseUmbrella(); 那样处理打开的雨伞;诚然,解释循环的代码更加令人困惑。
  • 现在很好的雨水处理程序。 ;-)
【解决方案2】:

“while”指令基本上是在 () 中的测试为真时将一段代码执行到 {} 中。

while(TEST)
{
    // any code you want to repeat while TEST is true
}

当您的程序点击“while”指令时,它将评估 TEST,如果 TEST 为真,那么它将运行括号中包含的代码。一旦程序到达括号中的最后一条指令,它将回到“while”的顶部并再次评估 TEST。只要 TEST 为真,这个过程就会重复。

在您的示例中,您希望代码执行 10 次,这样您就可以创建一个计数器变量,并在每次打印文本“hello world”时递增 1,如下所示:

int count = 0;
while(count < n)// this is true while count is inferior to n
{
     printf("hello world!");
     count++; // count = count + 1; 
     // count will progressively take the value 0,1,2,3,4,5,6,7,8,9 and 10
     // since 10 is equal to n (10) then the TEST (10 < 10) will return false and the "while" instruction will stop there
     // printing will not happen for count == n (10) but it will for 0, for a total of n times
}

【讨论】:

    【解决方案3】:

    while 循环是一种控制流语句,它允许代码根据给定的布尔条件重复执行。while 循环可以被认为是重复的 if 语句。

    语法: while(condition){ execute this }

    while循环里面的代码一直执行到条件为真为止。

    流程图:

    在上面的问题中,既然要控制台出Hello World! N 次,while(N--) 将起作用。

    示例代码:

    #include <iostream>
    using namespace std;
    int main() {
        int N;
        cin>>N;
        while(N--)
            cout<<"Hello World!\n";
        return 0;
    }
    

    注意:语句cout&lt;&lt;"Hello World!\n" 一直执行到N>=0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2017-01-20
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多