gpt4 book ai didi

Java程序无法运行

转载 作者:行者123 更新时间:2023-12-01 06:11:45 26 4
gpt4 key购买 nike

我的java程序无法正确计算折扣,我不明白为什么。我是java新手,不知道如何解决这个问题,所以任何帮助将不胜感激。例如,如果您购买 100 个包,您的折扣应该是 82.50,但我的说是 27.50

import java.text.DecimalFormat;
import java.util.Scanner;
public class Coffee {
public static void main(String args[]){
Scanner bags = new Scanner(System.in);
System.out.println("Hello and welcome to the program");
System.out.println("Here is the discount list for the IT125 Coffee Company");
System.out.println("<= 24 Bags No discount");
System.out.println(">= 25 Bags 5% ");
System.out.println(">= 50 Bags 10% ");
System.out.println(">= 100 Bags 15% ");
System.out.println(">= 150 Bags 20% ");
System.out.println(">= 200 Bags 25% ");
System.out.println(">= 300 Bags 30% ");
System.out.println("");

DecimalFormat df = new DecimalFormat("0.00");
int num1;
System.out.print("Enter the number of bags purchased: ");
num1 = bags.nextInt();
System.out.println("Bags purchased " + num1);
double cost, disc, totcost;
totcost = 0;
disc = 0;
cost = num1 * 5.50;
System.out.println("Normal Price: € " + df.format(cost));



if (num1 < 25) {
System.out.println("Your dicount is " + disc);
totcost = cost - disc;
System.out.print("Your total cost is € " + df.format(totcost));
}

else if (num1 >= 25) {
disc = cost * 0.05;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));
}

else if (num1 >= 50) {
disc = cost * 0.1;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));
}

else if (num1 >= 100) {
disc = cost * 0.15;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));
}

else if (num1 >= 150) {
disc = cost * 0.2;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));
}

else if (num1 >= 200) {
disc = cost * 0.25;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));

}

else if (num1 >= 300) {
disc = cost * 0.3;
totcost = cost - disc;
System.out.println("Your dicount is € " + df.format(disc));
System.out.print("Your total cost is € " + df.format(totcost));
}

}

}

最佳答案

万恶之源:

 else if (num1 >= 25) {

这会捕获从 25 到无穷大的所有数字。使用

 else if(num1 < 50) {

等等。在您分离完 300 以下的所有数量后,最后一个分支将不需要测试。

我想补充一点,代码重复并不好。避免这种情况的一种方法是使用

int[] limit = new int[]{ 25, 50, 100, 150, 200, 300 };
int[] disct = new int[]{ 5, 10, 15, 20, 25, 30 }

int d = 0;
for( int i = 0; i < limit.length; ++i ){
if( qtty < limit[i] ) break;
d = disct[i];
}

double cost = qtty*price;
double discount = cost*d/100;
totcost = cost - discount;

关于Java程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33055811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com