[英]C++ Converting a while loop to a do-while loop
我看到很多关于将for循环转换为while和do 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;
}
}
在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
并且仍然保留相同的功能,我的代码将不使用其他代码。
测试编号!= 0,然后再分配产品=产品*数字
否则,当用户输入0时,您乘以0并退出循环
问题是在do-while
版本中,当用户输入0时,此行:
product = product * number;
被执行,而在while
版本中则不执行。
因此,乘积将始终为0。
如果数字为0,则不要product
。
您的问题是退出时输入的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;
}
}
如果您只想解决答案为0的问题,则该问题归因于以下几行
cin >> number;
product = product * number;
您将数字取0并将其乘以乘积,显然结果将为0。
您可以通过放置break语句来解决它。
cin >> number;
if(number == 0)
break;
product = product * number;
但是,总的来说,我不确定您通过尝试解决此问题即将一会儿转换为一会儿会实现什么目标。
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,并且仅总是输出乘积。
无论您获得什么输入,您都在增加count
。 首先检查输入是否为零(您的中止输入)。
更改
count++
至
if (number)
count++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.