Java理想体重计算器:如何让JOptionPane仅接受数值输入?
问题:如何让JOptionPane仅接受数值类型输入
我正在开发一个基于性别计算理想体重的Java简易程序,在通过JOptionPane获取身高的环节,需要该组件仅接受数值类型输入。
当前使用的代码如下:
public class IdealWeightCalculator { public static void main(String[] args) { String gender= ""; do { gender= JOptionPane.showInputDialog("Enter your gender (M/F)"); } while(gender.equalsIgnoreCase("M") == false && gender.equalsIgnoreCase("F") == false); int height= Integer.parseInt(JOptionPane.showInputDialog("Enter your height in centimeters.")); int idealWeight = 0; if (gender.equalsIgnoreCase("M")) { idealWeight = height - 110; } else if(gender.equalsIgnoreCase("F")) { idealWeight = height - 120; } System.out.println("Your ideal weight is " + idealWeight + " units of love."); } }
当输入字符串而非数字时,程序会抛出异常并停止运行。我尝试用以下代码解决该问题:
while (!height.hasNextInt()) { System.out.println("Enter a number, please."); height.nextLine(); }
但出现错误提示:Cannot invoke hasNextInt() on the primitive type int。请问如何实现让JOptionPane仅接受数值输入的需求?
错误原因分析
hasNextInt()是Scanner类的专属方法,仅能用于Scanner对象调用;而你定义的height是int基本数据类型,不存在可调用的方法,因此报错。JOptionPane的输入返回值是字符串,需要针对这个字符串做合法性校验。
解决方案1:循环输入+异常捕获
针对JOptionPane的场景,最直接的方式是通过循环接收输入字符串,尝试将其转换为整数。若转换失败(抛出NumberFormatException),则重新弹出输入框,直到输入合法。
修改后的完整代码:
import javax.swing.JOptionPane; public class IdealWeightCalculator { public static void main(String[] args) { String gender = ""; do { gender = JOptionPane.showInputDialog("Enter your gender (M/F)"); } while (!gender.equalsIgnoreCase("M") && !gender.equalsIgnoreCase("F")); int height = 0; boolean validInput = false; // 循环校验身高输入 while (!validInput) { String heightInput = JOptionPane.showInputDialog("Enter your height in centimeters."); try { // 尝试转换为整数 height = Integer.parseInt(heightInput); // 额外校验身高合理性(可选) if (height > 0) { validInput = true; } else { JOptionPane.showMessageDialog(null, "Height must be a positive number!"); } } catch (NumberFormatException e) { // 转换失败,提示重新输入 JOptionPane.showMessageDialog(null, "Please enter a valid number!"); } } int idealWeight = gender.equalsIgnoreCase("M") ? height - 110 : height - 120; JOptionPane.showMessageDialog(null, "Your ideal weight is " + idealWeight + " units of love."); } }
解决方案2:正则表达式校验
也可以用正则表达式先验证输入字符串是否为正整数,再进行类型转换,避免异常捕获:
import javax.swing.JOptionPane; public class IdealWeightCalculator { public static void main(String[] args) { String gender = ""; do { gender = JOptionPane.showInputDialog("Enter your gender (M/F)"); } while (!gender.equalsIgnoreCase("M") && !gender.equalsIgnoreCase("F")); int height = 0; boolean validInput = false; while (!validInput) { String heightInput = JOptionPane.showInputDialog("Enter your height in centimeters."); // 正则匹配正整数(包含0的话可调整为"\\d*") if (heightInput != null && heightInput.matches("\\d+")) { height = Integer.parseInt(heightInput); validInput = true; } else { JOptionPane.showMessageDialog(null, "Please enter a valid positive number!"); } } int idealWeight = gender.equalsIgnoreCase("M") ? height - 110 : height - 120; JOptionPane.showMessageDialog(null, "Your ideal weight is " + idealWeight + " units of love."); } }
内容的提问来源于stack exchange,提问作者SandroFlex
相关产品推荐
相关产品推荐

