不懂 while 循环语法
Dont understand while loop syntax
我需要有关输入驱动循环的帮助,这意味着用户输入两个值并打印和、差、乘积、商和余数,直到用户为第二个值输入零。我不明白如何编写 while 循环 我正在测试什么变量
这是一个示例:
enter two integers: 19 7
sum of 19 and 7 is 26
difference of 19 and 7 is 12
etc..
我假设您是初学者...并且您正在使用 <iostream>
。如果您正在使用其他东西,例如<cstdio>
然后评论,我会更改代码,但它是这样的:(对于乘法,你可以算出其余的:D)
#include <iostream>
using namespace std;
int main (){
int num1;
int num2;
while (true){
cout << "Enter some numbers";
cin >> num1 >> num2;
cout << "Product is " << num1*num2;
}
return 0;
}
祝你编码顺利!
你可以尝试这样的事情。使用无限循环并根据自己的情况打破循环。
#include <iostream>
using namespace std;
int main (){
int num1, num2;
while (1){
cout << "\nEnter some numbers ";
cin >> num1 >> num2;
if(num2==0)
break;
cout << "Product is " << num1*num2;
}
return 0;
}
使用带有 break
条件的无限循环就可以了。这是
#include<iostream>
int main()
{
int a , b;
while( 1 )
{
std :: cout << "\nEnter two integers :" ;
std :: cin >> a >> b ;
if ( b == 0 )
break;
std :: cout << "\nSum of " << a << " and " << b << " is " << a + b ;
std :: cout << "\nDifference of " << a << " and " << b << " is " << a - b ;
std :: cout << "\nProduct of " << a << " and " << b << " is " << a * b ;
std :: cout << "\nQuotient when " << a << " is divided by " << b << " is " << a / b ;
std :: cout << "\nRemainder when " << a << " is divided by " << b << " is " << a % b ;
}
return 0;
}
如果 b == 0
,您只需使用 break
。当遇到break
时,程序退出循环。 (如果你是个菜鸟,不知道如何使用 break,请阅读这篇 break )
while( 1 )
是死循环,只有遇到break
程序才会退出循环
另请记住,%
不适用于浮点数。如果你想要浮动,那么你将不得不使用 std::fmod() ( std :: fmod( a , b );
returns a
除以 b
的余数,其中 a
和 b
是浮点数或双精度数,它包含在 <cmath>
头文件中)。
这可以通过多种方式完成。例如
while ( true )
{
std::cout << "Enter two integer numbers: ";
int first;
int second = 0;
std::cin >> first >> second;
if ( second == 0 ) break;
std::cout << "sum of " << first
<< " and " << second
<< " is " << first + second;
<< std::endl;
std::cout << "difference of " << first
<< " and " << second
<< " is " << first - second;
<< std::endl;
// and other outputs if they are required
}
我需要有关输入驱动循环的帮助,这意味着用户输入两个值并打印和、差、乘积、商和余数,直到用户为第二个值输入零。我不明白如何编写 while 循环 我正在测试什么变量
这是一个示例:
enter two integers: 19 7
sum of 19 and 7 is 26
difference of 19 and 7 is 12
etc..
我假设您是初学者...并且您正在使用 <iostream>
。如果您正在使用其他东西,例如<cstdio>
然后评论,我会更改代码,但它是这样的:(对于乘法,你可以算出其余的:D)
#include <iostream>
using namespace std;
int main (){
int num1;
int num2;
while (true){
cout << "Enter some numbers";
cin >> num1 >> num2;
cout << "Product is " << num1*num2;
}
return 0;
}
祝你编码顺利!
你可以尝试这样的事情。使用无限循环并根据自己的情况打破循环。
#include <iostream>
using namespace std;
int main (){
int num1, num2;
while (1){
cout << "\nEnter some numbers ";
cin >> num1 >> num2;
if(num2==0)
break;
cout << "Product is " << num1*num2;
}
return 0;
}
使用带有 break
条件的无限循环就可以了。这是
#include<iostream>
int main()
{
int a , b;
while( 1 )
{
std :: cout << "\nEnter two integers :" ;
std :: cin >> a >> b ;
if ( b == 0 )
break;
std :: cout << "\nSum of " << a << " and " << b << " is " << a + b ;
std :: cout << "\nDifference of " << a << " and " << b << " is " << a - b ;
std :: cout << "\nProduct of " << a << " and " << b << " is " << a * b ;
std :: cout << "\nQuotient when " << a << " is divided by " << b << " is " << a / b ;
std :: cout << "\nRemainder when " << a << " is divided by " << b << " is " << a % b ;
}
return 0;
}
如果 b == 0
,您只需使用 break
。当遇到break
时,程序退出循环。 (如果你是个菜鸟,不知道如何使用 break,请阅读这篇 break )
while( 1 )
是死循环,只有遇到break
程序才会退出循环
另请记住,%
不适用于浮点数。如果你想要浮动,那么你将不得不使用 std::fmod() ( std :: fmod( a , b );
returns a
除以 b
的余数,其中 a
和 b
是浮点数或双精度数,它包含在 <cmath>
头文件中)。
这可以通过多种方式完成。例如
while ( true )
{
std::cout << "Enter two integer numbers: ";
int first;
int second = 0;
std::cin >> first >> second;
if ( second == 0 ) break;
std::cout << "sum of " << first
<< " and " << second
<< " is " << first + second;
<< std::endl;
std::cout << "difference of " << first
<< " and " << second
<< " is " << first - second;
<< std::endl;
// and other outputs if they are required
}