-1

我看到很多关于将 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; 
} 

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

7 回答 7

3

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的并且仍然保留相同的功能。

于 2013-10-28T21:39:50.283 回答
2

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

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

于 2013-10-28T21:39:24.230 回答
2

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

product = product * number;

被执行,而在while版本中没有。

因此,产品将始终为 0。

如果数字是 0 就不要相乘product

于 2013-10-28T21:39:27.763 回答
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;
    }
}
于 2013-10-28T21:40:26.463 回答
1

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

cin >> number; 
product = product * number; 

您将数字 0 与乘积相乘,显然结果将为 0。

您可以通过放置一个 break 语句来修复它。

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

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

于 2013-10-28T21:40:51.400 回答
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 从潜在输入中排除,并且总是输出一个产品。

于 2013-10-28T21:43:18.310 回答
0

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

改变

count++

if (number)
    count++;
于 2013-10-28T21:41:32.550 回答