如何摆脱我不断遇到的错误?

时间:2018-10-06 06:32:58

标签: java

我不知道出了什么问题或为什么它无法运行。我已经在这里待了好几个小时,我真的很需要帮助。这是我在编程班简介中的一个学校项目。非常感谢

所以基本上这就是应该发生的事情。您键入您的账单支付的小费金额。之后,将弹出一条消息,告诉您小费的百分比,并提供有关小费的小消息。每次尝试修复错误时,我都会不断收到不同的错误消息,然后又出现另一个错误消息

public static void main(String[] args) {
    String input1 = JOptionPane.showInputDialog (null,
            "Enter the Total Bill");
    double bill = Double.parseDouble(input1);
    String input2 = JOptionPane.showInputDialog(null,
            "How much tip did you leave?");
    double tipAmount = Double.parseDouble(input2);
    double tipPercent = (tipAmount / bill);
    if (tipPercent < 0 || tipPercent > 1) {
        System.out.println("I don't think that's that correct. ");
    } else if (tipPercent >= 0 || tipPercent <= 0.05 ) {
        System.out.println("You left a " + tipPercent * 100 "% tip. "
        + "\nBad sercice or enpty wallet? ");
    } else if (tipPercent > 0.05 && tipPercent <= 0.1 ) {
        System.out.println("You left a " + tipPercent * 100 "% tip. "
        + "\nI think you could have done better");
    } else if (tipPercent > 0.1 && tipPercent <= 0.2 ) {
        System.out.println("You left a " + tipPercent * 100 "% tip. "
        + "\nNot too bad.";
    } else if (tipPercent > 0.2 && tipPercent <= 0.3 ) {
        System.out.println("You left a " + tipPercent * 100 "% tip. "
        + "\nSomeone's a bit generous.";
    } else if (tipPercent > 0.3 ) {
        System.out.println("You left a " + tipPercent * 100 "% tip. "
        + "\nBIG MONEY!!.";
    JOptionPane.showMessageDialog(null, message);

    }        
}

1 个答案:

答案 0 :(得分:0)

您有两个主要问题:

  1. 未定义消息变量
  2. 最后3条System.out.println语句在关闭);时丢失了

只需少量重构即可正常工作:

public static void main(String[] args) {
    String input1 = JOptionPane.showInputDialog (null,
            "Enter the Total Bill");
    double bill = Double.parseDouble(input1);
    String input2 = JOptionPane.showInputDialog(null,
            "How much tip did you leave?");
    double tipAmount = Double.parseDouble(input2);
    double tipPercent = (tipAmount / bill);
    String message = getMessage(tipPercent); // you were missing message variable
    System.out.println(message);
    JOptionPane.showMessageDialog(null, message);

}

private static String getMessage(double tipPercent) {
    if (tipPercent < 0 || tipPercent > 1) {
        return "I don't think that's that correct. ";
    } else if (tipPercent >= 0 || tipPercent <= 0.05 ) {
        return "You left a " + tipPercent * 100 +"% tip. "
                + "\nBad sercice or enpty wallet? ";
    } else if (tipPercent > 0.05 && tipPercent <= 0.1 ) {
        return "You left a " + tipPercent * 100 +"% tip. "
                + "\nI think you could have done better";
    } else if (tipPercent > 0.1 && tipPercent <= 0.2 ) {
        return "You left a " + tipPercent * 100 +"% tip. "
                + "\nNot too bad.";
    } else if (tipPercent > 0.2 && tipPercent <= 0.3 ) {
        return "You left a " + tipPercent * 100 +"% tip. "
                + "\nSomeone's a bit generous.";
    } else if (tipPercent > 0.3 ) {
        return "You left a " + tipPercent * 100 +"% tip. "
                + "\nBIG MONEY!!.";

    }
    return "Ooops!!"; // edge case, when no if-else statement matches
}