如何让Java计算器模块的Return选项返回主功能选择界面?
问题与解决方案
我是仅学习编程两周的新手,当前编写的Java brickgame程序中,选择[2]Calculator进入计算器模块后,选择[6]Return时,使用break仅能退出计算器循环,但无法回到主界面的[1]Biography、[2]Calculator、[3]Bye选择菜单。我希望修改代码实现Return返回主选择界面的功能,目前已为此困扰2小时,急需帮助。
问题根源
主菜单仅执行一次,无循环逻辑,导致退出计算器后程序直接走到结束分支;同时代码重复创建Scanner实例、nextInt()后未处理换行符,会引发输入读取异常。
修改方案
- 给主菜单套上
while(true)循环,让用户可重复选择功能,直到选[3]Bye退出程序 - 只创建一个Scanner实例,避免资源浪费和输入冲突
- 在
nextInt()/next()后添加scanner.nextLine(),清除输入缓冲区的换行符,防止后续nextLine()读取空内容 - 调整计算器模块的退出提示,明确返回主菜单而非直接结束程序
修改后的完整代码
import java.util.Scanner; public class Brickgame { // 遵循Java类名规范,首字母大写 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 仅创建一个Scanner实例 String choice; while (true) { // 主菜单循环,实现重复选择 System.out.println("\nChoose the operation you want to do?"); System.out.print("[1] Biography \n[2] Calculator \n[3] Bye \nChoose the number: "); choice = scanner.nextLine(); if (choice.equals("1")) { System.out.println("Please use Underscore Character or _ as your spacebar! "); System.out.print("Please Enter your Name: "); String fname = scanner.next(); System.out.print("Please Enter your Age: "); String age = scanner.next(); System.out.print("Please Enter your Address: "); String address = scanner.next(); System.out.print("Please Enter your Birthplace: "); String birthplace = scanner.next(); System.out.print("Please Enter your Birthday: "); String birthday = scanner.next(); System.out.print("Please Enter your Favorite Food: "); String favoriteF = scanner.next(); System.out.print("Please Enter your Favorite Subject: "); String favoriteS = scanner.next(); System.out.print("Please Enter your Favorite Color: "); String favoriteC = scanner.next(); System.out.print("Please Enter your Hobby: "); String hobby = scanner.next(); System.out.print("Please Enter your Secret Talent: "); String sTalent = scanner.next(); scanner.nextLine(); // 处理next()后的换行符,避免后续输入异常 System.out.println("\nHello " + fname); System.out.println("You are " + age + " years old"); System.out.println("You lived at " + address); System.out.println("You are born in " + birthplace); System.out.println("Your birthday is on " + birthday); System.out.println("Your Favorite Food is " + favoriteF); System.out.println("Your Favorite Subject is " + favoriteS); System.out.println("Your Favorite Color is " + favoriteC); System.out.println("Your hobby is to " + hobby); System.out.println("Your Secret Talent is to " + sTalent); } else if (choice.equals("2")) { while (true) { System.out.println("\n[1] Addition \n[2] Subtraction \n[3] Multiplication \n[4] Division \n[5] Greater Than/Less Than \n[6] Return \nEnter the number you chose:"); String calcChoice = scanner.nextLine(); if (calcChoice.equals("1")) { System.out.print("Enter your first number: "); int a = scanner.nextInt(); System.out.print("Enter your second number: "); int b = scanner.nextInt(); int sum = a + b; System.out.println(a + " + " + b + " = " + sum); scanner.nextLine(); // 清除换行符 } else if (calcChoice.equals("2")) { System.out.print("Enter your first number: "); int a = scanner.nextInt(); System.out.print("Enter your second number: "); int b = scanner.nextInt(); int diff = a - b; System.out.println(a + " - " + b + " = " + diff); scanner.nextLine(); } else if (calcChoice.equals("3")) { System.out.print("Enter your first number: "); int a = scanner.nextInt(); System.out.print("Enter your second number: "); int b = scanner.nextInt(); int product = a * b; System.out.println(a + " * " + b + " = " + product); scanner.nextLine(); } else if (calcChoice.equals("4")) { System.out.print("Enter your first number: "); float c = scanner.nextFloat(); System.out.print("Enter your second number: "); float d = scanner.nextFloat(); float quotient = c / d; System.out.println(c + " / " + d + " = " + quotient); scanner.nextLine(); } else if (calcChoice.equals("5")) { System.out.print("Enter your first number: "); int e = scanner.nextInt(); System.out.print("Enter your second number: "); int f = scanner.nextInt(); if (e > f) { System.out.println(e + " is greater than " + f); } else if (e < f) { System.out.println(e + " is lesser than " + f); } else { System.out.println(e + " is same as " + f); } scanner.nextLine(); } else if (calcChoice.equals("6")) { System.out.println("\nReturning to main menu..."); break; // 退出计算器循环,回到主菜单循环 } else { System.out.println("Invalid choice! Please enter a number between 1-6."); } } } else if (choice.equals("3")) { System.out.println("\nThank you and Goodbye!"); break; // 退出主循环,结束程序 } else { System.out.println("\nInvalid choice! Please enter a number between 1-3."); } } scanner.close(); // 关闭Scanner,释放资源 } }
关键修改点说明
- 主菜单外层添加循环,确保执行完子功能后自动回到主选择界面
- 统一使用单个Scanner实例,避免多Scanner导致的输入混乱
- 处理
nextInt()后的换行符,解决后续nextLine()读取空值的问题 - 调整计算器退出逻辑,明确返回主菜单而非直接终止程序
- 修正类名、变量名,遵循Java编码规范
内容的提问来源于stack exchange,提问作者Charles Glenn Naria
相关产品推荐
相关产品推荐

