如何从一个方法中给出 2 种不同的数据类型

我想使用方法、if 语句和用户输入来做一个简单的初学者项目。我在使用 calc() 方法时遇到了问题。我如何在 java 中返回两种不同的数据类型,如果我不能,我该怎么做,仍然使用 more 方法以外的方法?



import java.util.Scanner; //allow user input


public class fourFunctionCalculator{

    public static void main(String[] args) {

        Scanner keyboardInput = new Scanner(System.in);


        System.out.print("Enter your first number:"); //get first number

        double num1 = keyboardInput.nextDouble();


        System.out.print("Enter your operator: ");     // get operator

        String name = keyboardInput.next(); //grabs everything user types until a space


        System.out.print("Enter your second number: ");  //get second number

        double num2 = keyboardInput.nextDouble();


        System.out.println(calc(num1,op,num2));

    }



//troublesome part is here

    public static double calc(double num1, String op, double num2){

        if (op == "+") {

            return (num1 + num2);

        }

        else if (op == "-") {

            return (num1 - num2);

        }

        else if (op == "*") {

            return (num1 * num2);

        }

        else if (op == "/") {

            return (num1 / num2);

        }

        else {

            return ("INVALID OPERATOR");

        }


    }

}


青春有我
浏览 135回答 3
3回答

海绵宝宝撒

您可以生成自定义异常,还需要在 if 验证中使用 .equals() 方法,否则它将无法工作。fourFunctionCalculator.javaimport java.util.Scanner; //allow user inputpublic class fourFunctionCalculator{    public static void main(String[] args) {        Scanner keyboardInput = new Scanner(System.in);        System.out.print("Enter your first number:"); //get first number        double num1 = keyboardInput.nextDouble();        System.out.print("Enter your operator: ");     // get operator        String name = keyboardInput.next(); //grabs everything user types until a space        System.out.print("Enter your second number: ");  //get second number        double num2 = keyboardInput.nextDouble();        try {            System.out.println(calc(num1,name,num2));        } catch (InvalidOperatorException e) {            System.out.println(e);        }    }    public static double calc(double num1, String op, double num2){        if (op.equals("+")) {            return (num1 + num2);        }        else if (op.equals("-")) {            return (num1 - num2);        }        else if (op.equals("*")) {            return (num1 * num2);        }        else if (op.equals("/")) {enter code here            return (num1 / num2);        }        throw new InvalidOperatorException("INVALID OPERATOR : " + op);    }}InvalidOperatorException.javapublic class InvalidOperatorException extends RuntimeException {    private static final long serialVersionUID = 1L;    public InvalidOperatorException(String errorMessage) {        super(errorMessage);    }}

心有法竹

我建议返回一个 OptionalDouble 对象作为有效或无效的占位符......Ex #1:  return OptionalDouble.of(num1 + num2); // first if outcomeEx #2:  return OptionalDouble.empty(); // for the last else那么您的 main(...) 方法需要类似于...System.out.println(calc(num1,op,num2).orElseThrow(() -> new IllegalArgumentException("INVALID OPERATOR")));

大话西游666

I suggest returning always String. public static String calc(double num1, String op, double num2){            if (op == "+") {                return String.valueOf(num1 + num2);            }            else if (op == "-") {                return String.valueOf(num1 - num2);            }            else if (op == "*") {                return String.valueOf(num1 * num2);            }            else if (op == "/") {                return String.valueOf(num1 / num2);            }            else {                return ("INVALID OPERATOR");            }        }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java