【问题标题】:Java program not workingJava 程序不工作
【发布时间】:2015-10-11 00:23:05
【问题描述】:

我的 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));
        }

    }

}

【问题讨论】:

  • 您应该尝试将代码缩短为重现问题的最小示例。此外,显示所需的输出和问题输出。

标签: java


【解决方案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;

【讨论】:

  • 要么这样,要么重新排序 if 语句,以便最大的数字在前。
  • ...但绝不是两者兼而有之。
【解决方案2】:

您已将if 块按顺序排列,这样任何大于25 的块都会被第二个块捕获,即使它大到足以被后面的一个块捕获。 Java 不会检查else if 的列表;它几乎是一行一行的,所以如果它看到350 作为输入,它会看到它大于25 并进入那个if 块,即使它可以进入后面的块。如果您颠倒您的if 块的顺序,以便首先检查最大的数字,它将正常工作。

【讨论】:

    【解决方案3】:

    请尝试以下代码:添加边界条件。

    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
    

    【讨论】:

    • 哦,废话,我试图给你投票,但现在它不允许我改变它。抱歉……
    【解决方案4】:

    我可以看到你的问题。

    它在您的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语句中加入下限和上限,除了最后一个。

    【讨论】:

    • 您只是推迟了错误的阈值。如果没有额外的预防措施,你就不能拥有num1 &gt;= 50...
    • 我知道还有更多 if 块,但我的解释应该是为了帮助 OP 理解问题,而不是为他们编写所有代码。
    • 好的,但不要在答案中留下误导性代码 sn-ps。你有“正确的......将是”标题错误的代码,恕我直言,这是一个禁忌。
    猜你喜欢
    • 2011-08-15
    • 2015-08-06
    • 2014-02-21
    • 1970-01-01
    • 2017-12-14
    • 2012-07-16
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多