You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java球体计算器输入参数时抛出NumberFormatException问题求助

问题描述

我正在开发一款计算器,支持基础四则运算和球体体积/表面积计算。当输入命令sphere volume 5时,程序抛出如下错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "volume"

附上当前使用的代码:

public class SimpleCalculator {
    public static void main(String[] args) {
        String operator = args[0];
        int num1 = Integer.parseInt(args[1]);
        int num2 = Integer.parseInt(args[2]);
        double result;

        switch (operator) {
            case "+" :
                result = num1 + num2;
                System.out.println(result);
                break;
            
            case "-" :
                result = num1 - num2;
                System.out.println(result);
                break;

            case "*" :
                result = num1 * num2;
                System.out.println(result);
                break;
            
            case "/" :
                double dnum1 = num1;
                result = dnum1 / num2;
                System.out.println(result);
                break;
            
            case "sphere" :
            
            if (args[1] == "volume") {
                result = (4.0/3)* Math.PI * Math.pow(num2, 3);
                System.out.println(result);
                break;
            } else if (args[1] == "surface"){
                result = 4.0 * Math.PI * Math.pow(num2, 2);
                System.out.println(result);
                break;
            }
            break;
        }
        
    }
}

我最初的代码版本(曾用String变量区分计算类型):

case "sphere" :
    String calcType = args[1];
    if (calcType.equals("volume")) {
        result = (4.0/3)* Math.PI * Math.pow(num2, 3);
        System.out.println(result);
        break;
    } else if (calcType.equals("surface")){
        result = 4.0 * Math.PI * Math.pow(num2, 2);
        System.out.println(result);
        break;
    }
    break;
错误原因
  1. 参数解析时机错误:程序开头直接执行int num1 = Integer.parseInt(args[1]);,但当输入sphere volume 5时,args[1]是字符串"volume",并非数字,强行转换为整数必然触发NumberFormatException。
  2. 字符串比较方式错误:当前代码中用args[1] == "volume"比较字符串,Java里==对比的是对象引用地址,而非字符串内容,正确的做法是使用equals()方法(你最初的代码版本这里是对的)。
修正方案

将数字解析逻辑移到对应运算符的分支中,避免提前解析非数字参数;同时保留equals()的正确字符串比较方式。修正后的完整代码如下:

public class SimpleCalculator {
    public static void main(String[] args) {
        String operator = args[0];
        double result;

        switch (operator) {
            case "+" :
                int addNum1 = Integer.parseInt(args[1]);
                int addNum2 = Integer.parseInt(args[2]);
                result = addNum1 + addNum2;
                System.out.println(result);
                break;
            
            case "-" :
                int subNum1 = Integer.parseInt(args[1]);
                int subNum2 = Integer.parseInt(args[2]);
                result = subNum1 - subNum2;
                System.out.println(result);
                break;

            case "*" :
                int mulNum1 = Integer.parseInt(args[1]);
                int mulNum2 = Integer.parseInt(args[2]);
                result = mulNum1 * mulNum2;
                System.out.println(result);
                break;
            
            case "/" :
                int divNum1 = Integer.parseInt(args[1]);
                int divNum2 = Integer.parseInt(args[2]);
                double dNum1 = divNum1;
                result = dNum1 / divNum2;
                System.out.println(result);
                break;
            
            case "sphere" :
                String calcType = args[1];
                int radius = Integer.parseInt(args[2]);
                if (calcType.equals("volume")) {
                    result = (4.0/3)* Math.PI * Math.pow(radius, 3);
                    System.out.println(result);
                } else if (calcType.equals("surface")){
                    result = 4.0 * Math.PI * Math.pow(radius, 2);
                    System.out.println(result);
                }
                break;
        }
    }
}

内容的提问来源于stack exchange,提问作者konjac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:27:40