【问题标题】:C++ Converting a while loop to a do-while loopC ++将while循环转换为do-while循环
【发布时间】:2013-10-28 21:28:20
【问题描述】:

我看到很多关于将 for 循环转换为 while 并执行 while 循环的问题,但我似乎找不到任何关于在 C++ 中将 while 循环转换为执行 while 循环的内容。它仍然需要保持与原始代码相同的功能。

这是原始代码:

int number, product = 1, count = 0; 

cout << "Enter a whole number to be included in the product" 
<< endl << "or enter 0 to end the input: "; 
cin >> number; 

while (number != 0) 
{
product = product * number; 
count++; 
cout << "Enter a whole number to be included in the product" 
<< endl << "or enter 0 to end the input: "; 
cin >> number; 
} 

if (count > 0) 
{ 
cout << endl << "The product is " << product << "." << endl; 
} 

通过移动它,我能够在这里结束,但我一直以错误结束。 当我运行程序时,它会提示我输入一个数字,如预期的那样。如果我输入一个有效数字,它会循环并问我同样的问题。然后当我输入0时,它像往常一样踢出,但无论之前输入的数字如何,它都显示产品为0。

这是我尝试将其调整为 do while 循环:

int main()
{   

int number, product = 1, count = 0; 

do
{
    cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number; 
    product = product * number; 
    count++; 
}

while (number != 0);
{
    if (count > 0) 
    cout << endl << "The product is " << product << "." << endl; 
}
}

【问题讨论】:

  • 什么错误,请具体说明。
  • 编辑了原始帖子以包含该信息
  • 主要区别是您在第一次检查条件之前执行主体。这可能需要在循环体内进行大量逻辑调整!
  • @Simsyy 无法发现显示错误的任何显着差异......
  • 对于初学者,总是验证您的输入是否成功在尝试读取之后。这通常会导致看起来像while (std::cin &gt;&gt; value) { ... } 的循环。到目前为止,我从未有任何 do { ... } while (...)-loop 将其保存到生产代码中(诚然,我从事专业编程大约 15 年,所以它最终可能会发生)。

标签: c++ loops while-loop do-while


【解决方案1】:

在do .. while 循环中插入0 时,首先将乘积乘以0,而不是存在。因此乘积总是 0。

将产品移到之前:

int count = -1, number = 1, product = 1; 
do
{
    count++;
    product = product * number; // you can use product *= number;
    cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number;
}    
while (number != 0);

if (count > 0) 
    cout << endl << "The product is " << product << "." << endl;

注意:我的代码没有使用额外的if,仍然保留了相同的功能。

【讨论】:

    【解决方案2】:

    测试编号!=0 之前分配产品 = 产品*编号

    否则,当用户输入 0 时,你乘以 0 并退出循环

    【讨论】:

    • 或更好:更改循环,使其通过读取用户输入结束
    【解决方案3】:

    问题是在do-while版本中,当用户输入0时这一行:

    product = product * number;
    

    被执行,而在while 版本中不被执行。

    因此,产品将始终为 0。

    如果数字为 0,请不要乘以 product。

    【讨论】:

      【解决方案4】:

      您的问题是退出时的number 输入为0,这使您的整个产品等于0。试试这个逻辑:

      #include <iostream>
      
      int main() {
      
          int number = 0, product = 1, count = 0;
      
          do {
              std::cout << "Enter a whole number to be included in the product" << std::endl << "or enter 0 to end the input: ";
              std::cin >> number;
              if (number > 0) {
                  product = product * number;
                  count++;
              }
          } while (number != 0);
          {
              if (count > 0)
                  std::cout << std::endl << "The product is " << product << "." << std::endl;
          }
      }
      

      【讨论】:

        【解决方案5】:

        如果你只是想解决得到 0 作为答案的问题,那么问题是由于以下几行

        cin >> number; 
        product = product * number; 
        

        你把数字0乘以乘积,结果显然是0。

        你可以通过添加一个 break 语句来修复它。

        cin >> number;
        if(number == 0)
        break;
        product = product * number;
        

        但是,总的来说,我不确定通过尝试解决此问题(即将时间转换为执行时间)来实现什么。

        【讨论】:

          【解决方案6】:
          int number, product = 1, count = 0; 
          
          cout << "Enter a whole number to be included in the product" 
               << endl << "or enter 0 to end the input: "; 
          cin >> number; // you enter a non-0 number here
          
          while (number != 0) // you now loop until you hit 0 ...
          {
              product = product * number; 
              count++; 
              cout << "Enter a whole number to be included in the product" 
                   << endl << "or enter 0 to end the input: "; 
              cin >> number; // you overwrite the non-0 number you had previously input
          } 
          
          if (count > 0) 
          { 
              cout << endl << "The product is " << product << "." << endl; 
          }
          

          在不知道程序的完整上下文的情况下,我在这里猜测,但您可能可以将其重写为:

          int number = 0, product = 1, count = 0; 
          
          do
          {
              cout << "Enter a whole number to be included in the product" 
                   << endl << "or enter 0 to end the input: ";
              cin >> number;
              if (number != 0)
              {
                  count++;
                  product *= number;
              } 
          } while (number != 0); // exit the loop when you have a non-zero entry
          
          if (count > 0) 
          { 
              cout << endl << "The product is " << product << "." << endl; 
          }
          

          或者...

          int number = 0, product = 1; 
          
          do
          {
              cout << "Enter a whole number to be included in the product" 
                   << endl << "or enter 0 to end the input: ";
              cin >> number;
              if (number != 0)
              {
                  product *= number;
              } 
          } while (number != 0); // exit the loop when you have a non-zero entry
          
          cout << endl << "The product is " << product << "." << endl; 
          

          您可以避免第二个条件,因为您从潜在输入中排除了 0,并且总是输出一个产品。

          【讨论】:

            【解决方案7】:

            无论你得到什么输入,你都在增加count。首先检查输入是否为零(您的中止输入)。

            改变

            count++
            

            到

            if (number)
                count++;
            

            【讨论】:

              猜你喜欢
              • 2015-02-16
              • 1970-01-01
              • 1970-01-01
              • 2021-05-22
              • 1970-01-01
              • 2018-11-21
              • 1970-01-01
              • 2012-01-18
              • 2018-05-09
              相关资源
              最近更新 更多