如何实现Java控制台计算器输出结果后自动循环重启?
解决Java控制台计算器循环计算的问题
嘿,这个需求其实很简单——你只需要把现有的计算逻辑套进一个循环里就行!目前你的程序跑完一次计算就直接结束了,因为main方法里的代码只执行一遍。下面给你两种实现方案,按需选择:
方案1:无限循环(持续计算直到强制终止)
这种方式会让计算器一直运行,直到你手动按Ctrl+C终止程序。我还顺便优化了几个小问题,比如处理除以0的情况,以及避免异常导致的死循环:
import java.util.Scanner; public class Calculator { // 顺便修正了类名,符合Java命名规范~ public static void main(String[] args) { Scanner test = new Scanner(System.in); // 用while(true)创建无限循环,每次计算完自动重启 while (true) { int x; int y; String c; try { System.out.println("Insert a number "); x = test.nextInt(); System.out.println("Insert an operator e.g * / + -"); c = test.next(); System.out.println("Insert another number"); y = test.nextInt(); if (c.equals("*")) { System.out.println("The total is " + x * y); } else if (c.equals("+")) { System.out.println("The total is " + (x + y)); } else if (c.equals("-")) { System.out.println("The total is " + (x - y)); } else if (c.equals("/")) { // 提前判断避免除以0的异常 if (y == 0) { System.out.println("Error: Can't divide by zero!"); continue; // 跳过本次计算,直接进入下一轮 } System.out.println("The total is " + (x / y)); } else { System.out.println("Invalid operator! Please use * / + -"); } // 加个分隔线,让输出更清晰 System.out.println("------------------------"); } catch (Exception e) { System.out.println("Please enter valid values!"); // 清空Scanner缓冲区,不然无效输入会导致死循环 test.nextLine(); System.out.println("------------------------"); } } } }
关键改动说明:
- 用
while(true)把整个计算流程包裹起来,完成一次计算后自动回到循环开头,重新请求输入。 - 新增了除以0的判断,避免程序抛出未处理的异常。
- 异常处理里加了
test.nextLine(),解决了用户输入非数字后,Scanner缓冲区残留无效内容导致的死循环问题。
方案2:可选择退出的循环(更友好)
如果希望用户可以主动选择是否继续计算,而不是只能强制终止,可以用一个布尔变量控制循环:
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner test = new Scanner(System.in); boolean keepCalculating = true; while (keepCalculating) { int x; int y; String c; try { System.out.println("Insert a number "); x = test.nextInt(); System.out.println("Insert an operator e.g * / + -"); c = test.next(); System.out.println("Insert another number"); y = test.nextInt(); if (c.equals("*")) { System.out.println("The total is " + x * y); } else if (c.equals("+")) { System.out.println("The total is " + (x + y)); } else if (c.equals("-")) { System.out.println("The total is " + (x - y)); } else if (c.equals("/")) { if (y == 0) { System.out.println("Error: Can't divide by zero!"); System.out.println("------------------------"); continue; } System.out.println("The total is " + (x / y)); } else { System.out.println("Invalid operator! Please use * / + -"); } // 询问用户是否继续 System.out.println("Do you want to calculate again? (y/n)"); String choice = test.next(); if (!choice.equalsIgnoreCase("y")) { keepCalculating = false; System.out.println("Exiting calculator..."); } else { System.out.println("------------------------"); } } catch (Exception e) { System.out.println("Please enter valid values!"); test.nextLine(); System.out.println("------------------------"); } } test.close(); // 用完记得关闭Scanner } }
这个方案的优势:
- 用户可以通过输入
n主动退出程序,体验更友好。 - 最后关闭了Scanner,符合资源管理的最佳实践。
不管选哪种方案,核心思路都是让计算逻辑重复执行,循环就是实现这个需求的关键~
内容的提问来源于stack exchange,提问作者Ovidiu Vacaru
相关产品推荐
相关产品推荐

