Java 创建配送和折扣计算器获取错误的数字
我正在尝试创建一个程序,提示用户输入他正在购买的衬衫数量,并根据他的订单确定应用的折扣和运输成本。我尝试了两种不同的方法,如第一个Java 创建配送和折扣计算器获取错误的数字,java,Java,我正在尝试创建一个程序,提示用户输入他正在购买的衬衫数量,并根据他的订单确定应用的折扣和运输成本。我尝试了两种不同的方法,如第一个if语句中所示,然后是其他方法。当我用一件衬衫测试它时,我得到的总数是29,无论出于什么原因,它都是29的倍数,而不是24.5,并且不加10或更少的运费 public static void main(String[] args) { Scanner input = new Scanner (System.in); Syst
if
语句中所示,然后是其他方法。当我用一件衬衫测试它时,我得到的总数是29,无论出于什么原因,它都是29的倍数,而不是24.5,并且不加10或更少的运费
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Please enter the number of shirts ordered:");
int shirts = input.nextInt();
int cost = 0;
float shirt = 24.95f;
if (shirts <=2) {
cost = (int) (shirt * cost );
}
if (shirts <=5)
cost = (int) (shirts*24.95f + 8.00f);
{
if (shirts <=10)
cost = (int) (shirts*24.95f + 5.00f);
if (shirts >=11)
cost = (int) (shirts*24.95f);
System.out.print(cost);
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“请输入订购的衬衫数量:”);
int=input.nextInt();
整数成本=0;
浮式衬衫=24.95f;
如果(p您的成本变量是int,而它应该是类似于float的值,并且无论何时输入为1,都不将结果强制转换为int remove(int),那么它将进入以下条件,并返回预期值29。(int)(1*24.95f+5.00f)=29
if(if程序的主要问题是缺少else if
语句
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Please enter the number of shirts ordered:");
int shirts = input.nextInt();
int cost = 0;
float shirt = 24.95f;
if (shirts <=2)
cost = (int) (shirt * cost );
else if (shirts <=5)
cost = (int) (shirts*24.95f + 8.00f);
else if (shirts <=10)
cost = (int) (shirts*24.95f + 5.00f);
else if (shirts >=11)
cost = (int) (shirts*24.95f);
System.out.print(cost);
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“请输入订购的衬衫数量:”);
int=input.nextInt();
整数成本=0;
浮式衬衫=24.95f;
如果(衬衫你需要一些else if
语句来结束投票,因为这些语句很琐碎/不太可能对未来的访问者有帮助。我会添加else if,但为什么我用1件衬衫测试它时得到29件呢?用你的调试器运行代码,看看发生了什么。如果只是为了学习如何使用IDE的调试器,你真的应该这么做。你应该因此,正确地缩进代码,以了解其结构。IDe可以通过一个键盘快捷键为您做到这一点。