我是新手并且正在学习编程,我无法让它“正确”地“破解”。 它主要是“打破”(最后如果),硬币不是0,5,10或25。该程序应该像自动售货机,只使用角钱,镍币,宿舍,当有人进入“ 0“计数硬币就停止了。
#include <stdio.h>
#define NICKEL 5
#define DIME 10
#define QUARTER 25
int main(void)
{
int coins;
int sum = 0;
printf("Insert Coins: ");
scanf("%d",&coins);
while (coins != 0){
sum += coins;
if (coins == NICKEL){
printf("Insert coins: ");
scanf("%d",&coins);}
else {continue;}
if (coins == DIME){
printf("Insert Coins: ");
scanf("%d",&coins);}
else {continue;}
if (coins == QUARTER){
printf("Insert Coins: ");
scanf("%d",&coins);}
else {continue;}
if (coins != NICKEL || coins != DIME || coins != QUARTER || coins !=0){
break;
printf("wrong coin");}
printf("the amount you put in: %d",sum);
}
答案 0 :(得分:0)
我认为你正试图这样做,
#include <stdio.h>
#define NICKEL 5
#define DIME 10
#define QUARTER 25
int main(void)
{
int coins;
int sum = 0;
do {
printf("Insert Coins:");
scanf("%d", &coins);
if(coins == NICKEL || coins == DIME || coins == QUARTER) {
sum += coins;
} else if(coins == 0) {
break;
} else {
printf("Wrong Coin\n");
}
}while(1); // Infinite Loop.
printf("The amount you put in %d\n", sum);
}
答案 1 :(得分:0)
问题在于你的in语句没有中断。
0 == 0 true 0!= 0 false
在控制语句中(如if-else,for,while)在条件检查中它总是评估条件,并根据结果,它做出决定,
在评估此while (coins != 0)
并导致循环终止后,当某人输入0而不是while (0 != 0)
时,您已写入while (0)