我试图计算字符的输入,例如:1 1 1 1 1 2 2 2 2 5 2 5 5 5 5 5 5 10 10 10,但是我无法摆脱循环并开始新的倒计时。
当我尝试仅在屏幕上记录总分时,总是在每个周期中重复cout,如何在周期结束时只获得全部金额?
如何更改代码以简化数字计算?
有什么建议吗?
int main(){
int cent = 0;
int cent_input = 0;
int cent_quantity = 0;
int downgrade_to_one = 0;
do{
while(true){
cout << "\nEnter number: " && cin >> cent;
switch(cent){
case 1:
downgrade_to_one = cent + downgrade_to_one;
if (cent == 0)
cent + 1;
cout << "\nYou have: " << downgrade_to_one << " one cent monets.";
break;
case 2:
cent_input = (cent_input + cent) / cent;
cent_quantity = cent_quantity + cent_input;
cout << "\nYou have: " << cent_quantity << " two cent monets.";
case 5:
cent_input = (cent_input + cent) / cent;
cent_quantity = cent_quantity + cent_input;
cout << "\nYou have: " << cent_quantity << " five cent monets.";
break;
case 10:
cent_input = (cent_input + cent) / cent;
cent_quantity = cent_quantity + cent_input;
cout << "\nYou have: " << cent_quantity << " ten cent monets.";
break;
case 25:
cent_input = (cent_input + cent) / cent;
cent_quantity = cent_quantity + cent_input;
cout << "\nYou have: " << cent_quantity << " twentyfive cent monets.";
break;
case 50:
cent_input = (cent_input + cent) / cent;
cent_quantity = cent_quantity + cent_input;
cout << "\nYou have: " << cent_quantity << " fifty cent monets.";
break;
default:
cout << "\nUndefined input!";
}
}
}
while(false);
}
答案 0 :(得分:0)
感谢所有拒绝了我的问题的人。我自己解决了这个问题。
int main(){
int cent = 0;
int one_cent_amount = 0;
int two_cent_amount = 0;
int five_cent_amount = 0;
int ten_cent_amount = 0;
int twentyfive_cent_amount = 0;
int fifty_cent_amount = 0;
int cent_input = 0;
int cent_quantity = 1;
int dollars = 0;
int cents = 0;
int entire_quantity = 0;
cout << "\nEnter number: ";
while(cin >> cent){
switch(cent){
case 1:
if(cent == 0)
cent + 1;
one_cent_amount = cent + one_cent_amount;
break;
case 2:
cent_input = (cent_input + cent) / cent;
two_cent_amount = two_cent_amount+ cent_input;
break;
case 5:
cent_input = (cent_input + cent) / cent;
five_cent_amount = five_cent_amount + cent_input;
break;
case 10:
cent_input = (cent_input + cent) / cent;
ten_cent_amount = ten_cent_amount + cent_input;
break;
case 25:
cent_input = (cent_input + cent) / cent;
twentyfive_cent_amount = twentyfive_cent_amount + cent_input;
break;
case 50:
cent_input = (cent_input + cent) / cent;
fifty_cent_amount = fifty_cent_amount + cent_input;
break;
default:
cout << "\nUndefined input!";
}
scanf("%c", ¢); //%c to read a character
if(cent == '\n') {
break;
}
}
entire_quantity = one_cent_amount + (two_cent_amount * 2) + (five_cent_amount * 5) + (ten_cent_amount * 10) + (twentyfive_cent_amount * 25) + (fifty_cent_amount * 50);
dollars = entire_quantity / 100;
cents = entire_quantity % 100;
if (entire_quantity != 1 && entire_quantity != 0)
cout << "\nYou have: " << one_cent_amount << " one cent monets."
<< '\n' << two_cent_amount << " two cent monets."
<< '\n' << five_cent_amount<< " five cent monets."
<< '\n' << ten_cent_amount << " ten cent monets."
<< '\n' << twentyfive_cent_amount << " twentyfive cent monets."
<< '\n' << fifty_cent_amount << " fifty cent monets."
<< '\n' << "Entire quantity: " << entire_quantity << " cents."
<< '\n' << "Your money are: " << dollars << " dollars and " << cents << " cents.";
else if (entire_quantity == 1)
cout << "\nYou have only " << entire_quantity << " cent.";
else if (entire_quantity == 0)
cout << "\nNothing is specified, you need to enter the amount!";
}