[英]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.