Java 我怎样才能摆脱不断犯下的错误?

Java 我怎样才能摆脱不断犯下的错误?,java,Java,我不知道是什么地方出了问题,为什么它不能运行。我已经在这里玩了好几个小时了,我真的很想得到一些帮助。这是我在《编程入门》课上的一个学校项目。非常感谢 所以基本上这就是应该发生的事情。你输入你的账单,支付小费的金额。之后会弹出一条消息,告诉你小费的百分比,并给出一条关于小费的小消息。每次尝试修复错误时,我都会收到不同的错误消息,另一条消息会出现 public static void main(String[] args) { String input1 = JOptionPane.show

我不知道是什么地方出了问题,为什么它不能运行。我已经在这里玩了好几个小时了,我真的很想得到一些帮助。这是我在《编程入门》课上的一个学校项目。非常感谢

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

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);

    }        
}
publicstaticvoidmain(字符串[]args){
字符串input1=JOptionPane.showInputDialog(null,
“输入账单总额”);
双账单=double.parseDouble(输入1);
字符串input2=JOptionPane.showInputDialog(null,
“你留了多少小费?”;
double tipAmount=double.parseDouble(input2);
双倾卸器=(tipAmount/bill);
如果(Tipercent<0 | | Tipercent>1){
System.out.println(“我认为这不对。”);
}否则如果(Tipercent>=0 | | Tipercent 0.05和&Tipercent 0.1和&Tipercent 0.2和&Tipercent 0.3){
System.out.println(“您留下了一个“+tipercent*100”%tip。“
+“\n大钱!!。”;
showMessageDialog(空,消息);
}        
}

您有两个主要问题:

  • 未定义消息变量
  • 最后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
    }
    
    publicstaticvoidmain(字符串[]args){
    字符串input1=JOptionPane.showInputDialog(null,
    “输入账单总额”);
    双账单=double.parseDouble(输入1);
    字符串input2=JOptionPane.showInputDialog(null,
    “你留了多少小费?”;
    double tipAmount=double.parseDouble(input2);
    双倾卸器=(tipAmount/bill);
    String message=getMessage(tipPercent);//缺少消息变量
    System.out.println(消息);
    showMessageDialog(空,消息);
    }
    私有静态字符串getMessage(双tipPercent){
    如果(Tipercent<0 | | Tipercent>1){
    return“我认为这不对。”;
    }否则如果(Tipercent>=0 | | Tipercent 0.05和&Tipercent 0.1和&Tipercent 0.2和&Tipercent 0.3){
    return“您留下了一个”+tipPercent*100+%tip。“
    +“\n大钱!!。”;
    }
    返回“Ooops!!”;//如果没有if-else语句匹配,则返回边缘大小写
    }
    
    欢迎来到Stack Overflow,谢谢您的提问。如果您能告诉我们您收到的确切错误消息,那将非常有帮助。
    您留下了“+tipercent*100”%tip。“
    这是无效的语法。。。。可能您的意思是:
    “您留下了一个“+(tipercent*100)+%tip。”
    如果您添加完整的错误消息,这将非常有用。人类不是编译器,没有信息很难发现问题。我真的很感激。我想不出来,但你们都帮了大忙。如果这个答案适合你,你没有问题了,请接受答案或结束这个问题。