不用if,break的方法
float number = 1, total = 0;
while (number > 0) {
cout << "请输入数字:";
cin >> number;
total += number;
cout << "当前输入所有数字的和为:" << total << endl;
}
不用if,break的方法
float number = 1, total = 0;
while (number > 0) {
cout << "请输入数字:";
cin >> number;
total += number;
cout << "当前输入所有数字的和为:" << total << endl;
}
用了另外一个方法
int total=0;
int a;
do
{
cout << "请输出一个数字:";
cin >> a;
total += a;
cout << "当前数字总和为:" << total << endl;
} while (a != 0);
while(true){
}
死循环
跳出xun'h
while(){
if(条件){
break;
}
}
total = 0; float num; do { cout << "Please enter a number:"; cin >> num; total += num; cout << total << endl; } while (num != 0);
7,编写一个程序,让用户可以持续输入数字,每次输入数字,报告当前所有输入的和。当用户输入0的时候,程序结束。
siki:
float total = 0;
while (true)
{
cout << "请输入一个数字:";
float number;
cin >> number;
if (number == 0)
{
break;
}
total += number;
cout << "当前所有输入的和为:" << total << endl;
}
自写:
bool execute = true;
float totalNum = 0;
while (execute)
{
float temp;
cout << "请输入一个数字:";
cin >> temp;
if (temp == 0)
{
execute = false;
}
totalNum += temp;
cout << "当前和为:" << totalNum << endl;
}