如何在switch语句的默认情况下将throw异常替换为System.out.print()?

huangapple go评论79阅读模式
英文:

How can I replace throw exception with a System.out.print() in switch statement default case

问题

以下是您要翻译的内容:

这里我有一个简单的类来执行计算器操作。

Calculator.java

    private final ArrayList <Double> values;
    private final String operation;

    public Calculator(ArrayList<Double> values, String operation) {
        this.values = values;
        this.operation = operation;
    }

    public double calculate(){
        double num1 = values.get(0);
        double num2 = values.get(1);
        double answer;

        switch (operation) {
            case "+" :
                answer = addition(num1,num2);
                break;
            case "-" :
                answer = subtraction(num1,num2);
                break;
            case "*" :
                answer = multiplication(num1,num2);
                break;
            case "/" :
                answer = division(num1,num2);
                break;
            default:
                // 在此处使用自定义的System.out语句,不返回任何内容并停止程序执行
                System.out.println("自定义错误消息:" + operation);
                return 0; // 此处使用返回0,因为这是一个计算器
        }
        return answer;
    }

    public double addition(double num1,double num2){
        return num1+num2;
    }
    public double subtraction(double num1,double num2){
        return num1-num2;
    }
    public double multiplication(double num1,double num2){
        return num1*num2;
    }
    public double division(double num1,double num2){
        return num1/num2;
    }
}

请注意,我已经将异常处理部分翻译成了中文,并替换了原来的异常抛出语句。

英文:

Here I have a simple class to perform calculator operations.

Calculator.java

    private final ArrayList <Double> values;
    private final String operation;

    public Calculator(ArrayList<Double> values, String operation) {
        this.values = values;
        this.operation = operation;
    }

    public double calculate(){
        double num1 = values.get(0);
        double num2 = values.get(1);
        double answer;

        switch (operation) {
            case "+" :
                answer = addition(num1,num2);
                break;
            case "-" :
                answer = subtraction(num1,num2);
                break;
            case "*" :
                answer = multiplication(num1,num2);
                break;
            case "/" :
                answer = division(num1,num2);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + operation);
        }
        return answer;
    }

    public double addition(double num1,double num2){
        return num1+num2;
    }
    public double subtraction(double num1,double num2){
        return num1-num2;
    }
    public double multiplication(double num1,double num2){
        return num1*num2;
    }
    public double division(double num1,double num2){
        return num1/num2;
    }
}

Here I want to replace throw new IllegalStateException("Unexpected value: " + operation);
this one with my own System.out the message and return nothing and stop program execution.

Here I can't use return 0 because this is a calculator.

Actually I want to know how to handle this exception properly with my own system out statement in cli without a red-letter exception message.

I have used JDK 14 here.

答案1

得分: 2

以下是您要翻译的内容:

[...] 并且不返回任何内容并停止程序执行。

由于该方法签名具有返回类型为 double,您必须返回一个结果。不能返回 没有结果

为此,您可以更改方法合同,将 calculate 方法的返回类型更改为 void,并通过单独的方法访问计算结果。

private final ArrayList<Double> values;
private final String operation;
private Double result; //用于存储结果的实例变量。

public void calculate() {
    Double num1 = values.get(0);
    Double num2 = values.get(1);
    switch (operation) {
        case "+" :
            result = addition(num1, num2);
            break;
        case "-" :
            result = subtraction(num1, num2);
            break;
        case "*" :
            result = multiplication(num1, num2);
            break;
        case "/" :
            result = division(num1, num2);
            break;
        default:
           //在此处添加您的打印语句
    }
 }

public Optional<Double> getResult() {
    return Optional.ofNullable(result);
}

这种方法的一个问题是当调用者在调用 calculate 之前调用 getResult() 时会发生什么情况。这部分我将留给您来处理。

英文:

>[...] and return noting and stop program execution.

Since the method signature has a return type of double you have to return a result. You cannot return with no result.

For that, you can change the method contract and make calculate method of return type void and access the calculated result via a separate method.

private final ArrayList &lt;Double&gt; values;
private final String operation;
private Double result; //an instance variable to store the result.

public void calculate() {
    Double num1 = values.get(0);
    Double num2 = values.get(1);
    switch (operation) {
        case &quot;+&quot; :
            result = addition(num1,num2);
            break;
        case &quot;-&quot; :
            result = subtraction(num1,num2);
            break;
        case &quot;*&quot; :
            result = multiplication(num1,num2);
            break;
        case &quot;/&quot; :
            result = division(num1,num2);
            break;
        default:
           //Your print statement here
    }
 }

public Optional&lt;Double&gt; getResult() {
    return Optional.ofNullable(result);
}

A problem with this is what happens when a caller calls getResult() before calculate. I'll leave that to you.

huangapple
  • 本文由 发表于 2020年8月10日 22:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63342471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定