我的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));
}
}
}
答案 0 :(得分:1)
万恶之源:
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;
答案 1 :(得分:0)
你已经将if
块放在一个顺序中,使得大于25
的任何东西都会被第二个阻塞,即使它足够大,可以被其中一个阻挡。 Java没有检查else if
的列表;它几乎是逐行的,所以如果它看到350
作为输入,它会看到它大于25
并进入if
块,即使它可以进入后来的一个。如果您颠倒订单,则会有if
块,以便首先检查最大数字,它将正常工作。
答案 2 :(得分:-1)
我可以看到你的问题。
它位于您的if
/ else
声明中。
想象一下,你将num1设为60。
您显然希望在下面执行代码C,但不幸的是,它会执行代码B,因为它是导致if
的第一个true
:
下面:
if (num1 < 25) {
//A
}
else if (num1 >= 25) {
//B
}
else if (num1 >= 50) {
//C
}
正确的if
块将是:
if (num1 < 25) {
//A
}
else if (num1 >= 25 && num<50) {
//B
}
else if (num1 >= 50) {
//C
}
这个例子并没有像你那样有很多条件,你需要在所有if
语句中设置下限和上限,除了最后一个。
答案 3 :(得分:-1)
请尝试使用以下代码:添加边界条件。
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 && num1<50) {
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 && num1<100) {
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 && num1<150) {
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 && num1<200) {
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 && num1<300) {
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));
}
}
}
输出:
Hello and welcome to the program
Here is the discount list for the IT125 Coffee Company
<= 24 Bags No discount
>= 25 Bags 5%
>= 50 Bags 10%
>= 100 Bags 15%
>= 150 Bags 20%
>= 200 Bags 25%
>= 300 Bags 30%
Enter the number of bags purchased: 100
Bags purchased 100
Normal Price: € 550.00
Your dicount is € 82.50
Your total cost is € 467.50