gpt4 book ai didi

java - 当用户输入负值时如何退出程序 - Java

转载 作者:行者123 更新时间:2023-11-30 02:46:30 25 4
gpt4 key购买 nike

扫描仪输入 = new 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com