Java Scanner输入:如何设置Quit终止条件与循环输入逻辑?
解决方案:实现任意阶段输入"Quit"终止+重复输入逻辑
因为你目前只掌握Scanner的基础输入方法,下面的实现完全不用方法和OOP,所有逻辑都写在main方法里,满足你的需求:
import java.util.Scanner; public class TemperatureConverter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 外层循环:实现重复输入,直到用户输入Quit while (true) { System.out.println("Please give three numbers (enter 'Quit' to exit at any time)"); // 读取第一个输入,先按字符串处理 String input1 = sc.next(); if (input1.equalsIgnoreCase("Quit")) { System.out.println("Program terminated."); break; } // 尝试转成整数,处理输入非数字的情况 int num1; try { num1 = Integer.parseInt(input1); } catch (NumberFormatException e) { System.out.println("Invalid input! Please enter a number or 'Quit'."); // 跳过当前循环剩余部分,重新开始输入 continue; } // 处理第二个数字 String input2 = sc.next(); if (input2.equalsIgnoreCase("Quit")) { System.out.println("Program terminated."); break; } int num2; try { num2 = Integer.parseInt(input2); } catch (NumberFormatException e) { System.out.println("Invalid input! Please enter a number or 'Quit'."); continue; } // 处理第三个数字 String input3 = sc.next(); if (input3.equalsIgnoreCase("Quit")) { System.out.println("Program terminated."); break; } int num3; try { num3 = Integer.parseInt(input3); } catch (NumberFormatException e) { System.out.println("Invalid input! Please enter a number or 'Quit'."); continue; } // 选择原始温度单位 System.out.println("Choose the temperature unit of the three numbers."); System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin (enter 'Quit' to exit)"); String unitInput = sc.next(); if (unitInput.equalsIgnoreCase("Quit")) { System.out.println("Program terminated."); break; } int tempUnit; try { tempUnit = Integer.parseInt(unitInput); } catch (NumberFormatException e) { System.out.println("Invalid input! Please enter 1/2/3 or 'Quit'."); continue; } // 验证单位输入是否合法 if (tempUnit < 1 || tempUnit > 3) { System.out.println("Invalid unit! Please choose 1, 2 or 3."); continue; } switch (tempUnit) { case 1 -> System.out.println("Celsius was chosen."); case 2 -> System.out.println("Fahrenheit was chosen."); case 3 -> System.out.println("Kelvin was chosen."); } // 选择目标温度单位 System.out.println("Choose the temperature unit you want to convert it into."); System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin (enter 'Quit' to exit)"); String targetInput = sc.next(); if (targetInput.equalsIgnoreCase("Quit")) { System.out.println("Program terminated."); break; } int chosenTemp; try { chosenTemp = Integer.parseInt(targetInput); } catch (NumberFormatException e) { System.out.println("Invalid input! Please enter 1/2/3 or 'Quit'."); continue; } if (chosenTemp < 1 || chosenTemp > 3) { System.out.println("Invalid unit! Please choose 1, 2 or 3."); continue; } switch (chosenTemp) { case 1 -> System.out.println("Celsius was chosen as target."); case 2 -> System.out.println("Fahrenheit was chosen as target."); case 3 -> System.out.println("Kelvin was chosen as target."); } // 这里可以添加温度转换逻辑,根据你的需求补充 System.out.println("--- Conversion completed, ready for new input ---"); } sc.close(); } }
关键逻辑说明
为什么不用
nextInt()直接读取?
如果你直接用nextInt(),当用户输入"Quit"时,Scanner会抛出InputMismatchException(因为"Quit"不是整数),程序会崩溃。所以必须先把所有输入按字符串读取,先判断是否是"Quit",再尝试转成整数。循环重复输入的实现
用while(true)创建一个无限循环,每次输入完成或者输入错误时,都会回到循环开头,让用户重新输入;只有当用户输入"Quit"时,用break跳出循环,终止程序。输入错误处理
用try-catch处理字符串转整数失败的情况(比如输入字母、符号),提示用户重新输入,避免程序崩溃。同时验证温度单位的输入是否在1-3之间,不符合要求也提示重新输入。
你之前代码的问题
你写的if (!sc.hasNextInt())逻辑有漏洞:只有当Scanner检测到下一个输入不是整数时才会进入判断,但如果用户在nextInt()之前输入"Quit",nextInt()会直接报错,根本走不到这个判断里。所以必须先读取字符串,再做判断。
内容的提问来源于stack exchange,提问作者Sotei
相关产品推荐
相关产品推荐

