C ++将while循环转换为do-while循环

时间:2013-10-28 21:28:20

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

我看到很多关于将for循环转换为while和while循环的问题,但是我似乎无法在转换while循环时找到任何东西在C ++中执行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; 
} 

通过移动它,我能够在这里结束,但我总是以错误结束。 当我运行程序时,它会提示我输入一个while数字,就像预期的那样。如果我输入一个有效的数字,它会循环并询问我同样的问题。然后,当我输入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; 
}
}

7 个答案:

答案 0 :(得分:3)

在插入0时的do .. while循环中,首先将乘积乘以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,但仍保留相同的功能。

答案 1 :(得分:2)

测试号码!= 0,然后再分配产品=产品* nuumber

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

答案 2 :(得分:2)

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

product = product * number;

已执行,而while版本未执行。

因此,产品将始终为0。

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

答案 3 :(得分:2)

您的问题是退出时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;
    }
}

答案 4 :(得分:1)

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

cin >> number; 
product = product * number; 

你取0的数字并乘以产品,结果显然是0。

你可以通过发表一个中断声明来修复它。

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

但是,总的来说,我不确定你是通过尝试解决这个问题来实现的,即将一段时间转换为一段时间。

答案 5 :(得分:1)

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,并且只是始终输出产品。

答案 6 :(得分:0)

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

更改

count++

if (number)
    count++;