扫描仪输入=新的Scanner(System.in);

    System.out.print("How much can you afford for meals per day? Enter it now ");
    double mealAmount = input.nextDouble();

最佳答案

这是因为仅在获得3个double值之后才执行测试。借助于input.nextDouble(),您应该在获得每个变量的值后立即移动对应的测试。

您的代码应该是:

System.out.print("How much can you afford for meals per day? Enter it now ");
double mealAmount = input.nextDouble();

if (mealAmount <0)
    System.exit(0);
else if (mealAmount < 15.00 && mealAmount >=0)
    System.out.println("No meal for you. Tough luck.");
else if (mealAmount >= 15.00 && mealAmount < 30.00)
    System.out.println("You can afford bronze meals.");
else if (mealAmount >= 30.00 && mealAmount < 60.00)
    System.out.println("You can afford silver meals.");
else if (mealAmount >= 60.00)
    System.out.println("You can afford gold meals.");
...


注意:无需显式调用System.exit(0),只需使用return

关于java - 用户输入负值时如何退出程序-Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40048560/

10-12 17:50