C 这种改变不是';行不通
这是我的代码。出于某种原因,它总是说四分之一的数量是四分之一。我做错了什么C 这种改变不是';行不通,c,C,这是我的代码。出于某种原因,它总是说四分之一的数量是四分之一。我做错了什么 #include <stdio.h> #define QUARTER 25 // Define the constant cent values of the quarters, dimes, nickels, and pennies. #define DIME 10 #define NICKEL 5 #define PENNY 1 int main( void ){ in
#include <stdio.h>
#define QUARTER 25 // Define the constant cent values of the quarters, dimes, nickels, and pennies.
#define DIME 10
#define NICKEL 5
#define PENNY 1
int main( void ){
int priceOfitem; // Initialize the variable that will be the price.
printf("Enter the price of an item less than one dollar (in cents) (eg: 57) : ");
scanf("%lg", &priceOfitem);
if(priceOfitem >= 100){
printf("The price must be less than one dollar. \nProgramming exiting.");
return 0;
}
int changeAmount = 100 - priceOfitem; /* Create the variable that is the amount of change needed.
* This variable will be modified later on.
*/
int amountOfQuarters = ((changeAmount - (changeAmount % QUARTER)) / QUARTER); // Utilizing the modulus operator to determine the amount of quarters.
printf("\n\nThe Amount of Quarters Needed in the Change is: %d", amountOfQuarters);
changeAmount = changeAmount - (amountOfQuarters * QUARTER); // Modifying the change amount
int amountOfDimes = ((changeAmount - (changeAmount % DIME)) / DIME); // Utilizing the modulus operator to determine the amount of dimes.
printf("\n\nThe Amount of Dimes Needed in the Change is: %d", amountOfDimes);
changeAmount = changeAmount - (amountOfDimes * DIME); // Modifying the change amount
int amountOfNickels = ((changeAmount - (changeAmount % NICKEL)) / NICKEL); // Utilizing the modulus operator to determine the amount of nickels.
printf("\n\nThe Amount of Nickels Needed in the Change is: %d", amountOfNickels);
changeAmount = changeAmount - (amountOfNickels * NICKEL); // Modifying the change amount
int amountOfPennies = changeAmount; // Since the changeAmount can now be expressed with pennies only, set the amountOfPennies variable as such.
printf("\n\nThe Amount of Pennies Needed in the Change is: %d", amountOfPennies);
return 0;
}
#包括
#定义25美分//定义25美分、10美分、5美分和1美分的固定美分值。
#定义10美分
#定义镍5
#定义便士1
内部主(空){
int priceOfitem;//初始化将作为价格的变量。
printf(“输入低于一美元(以美分为单位)的物品的价格(例如:57):”;
scanf(“%lg”和priceOfitem);
如果(价格项目>=100){
printf(“价格必须低于一美元。\n正在退出编程。”);
返回0;
}
int changeamum=100-priceOfitem;/*创建所需更改量的变量。
*稍后将修改此变量。
*/
int AmountOquarters=((changeAmount-(changeAmount%QUARTER))/QUARTER);//使用模数运算符确定四分之一的数量。
printf(“\n\n更改所需的季度数为:%d”,amountFQuarters);
changeAmount=changeAmount-(amOnTofquarters*QUARTER);//修改更改金额
int amountoftimes=((changeamunt-(changeamunt%DIME))/DIME);//利用模运算符确定DIME的数量。
printf(“\n\n更改所需的一角硬币数量为:%d”,amountoftimes);
changeAmount=changeAmount-(amountofTimes*DIME);//修改更改金额
int amountOfNickels=((changeamunt-(changeamunt%NICKEL))/NICKEL);//利用模数运算符确定NICKEL的数量。
printf(“\n\n更改所需的镍币数量为:%d”,数量为50镍币);
changeAmount=changeAmount-(amountOfNickels*镍);//修改更改金额
int amountOfPennies=changeAmount;//因为changeAmount现在只能用便士表示,所以将amountOfPennies变量设置为这样。
printf(“\n\n更改所需的便士数量为:%d”,amountOfPennies);
返回0;
}
此处:
int priceOfitem; // Initialize the variable that will be the price.
printf("Enter the price of an item less than one dollar (in cents) (eg: 57) : ");
scanf("%lg", &priceOfitem);
对于整数输入,您需要将“%d”
传递给scanf:
scanf("%d", &priceOfitem);
此外,您不需要此减法:
int changeAmount = priceOfitem; //int changeAmount = 100 - priceOfitem
编译时,请始终使用
-Wall-Werror
(或选择编译器时使用的类似标志)。这并不是对您问题的真正回答,但
通常我们用整数除法计算四分之一(以及类似的东西)的数量,这就扔掉了任何小数部分
int amountOfQuarters = changeAmount / QUARTER;
所以,如果changeAmount是57,我们用整数算法除以25,我们得到2。请问,问题在哪里?这不是一个代码审查的网站。@JensGustedt,我认为问题很清楚:
出于某种原因,它总是说季度数是4。我做错了什么?
非常感谢!我犯了一个我忽略的简单错误,结果在我的程序中导致了一个巨大的逻辑错误!谢谢