为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

青春有我 2023-03-02 16:14:58
我想使用方法、if 语句和用户输入来做一个简单的初学者项目。我在使用 calc() 方法时遇到了问题。我如何在 java 中返回两种不同的数据类型,如果我不能,我该怎么做,仍然使用 more 方法以外的方法?import 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();        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");        }    }}
查看完整描述

3 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

您可以生成自定义异常,还需要在 if 验证中使用 .equals() 方法,否则它将无法工作。


fourFunctionCalculator.java


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();

        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.java


public class InvalidOperatorException 

extends RuntimeException {

    private static final long serialVersionUID = 1L;


    public InvalidOperatorException(String errorMessage) {

        super(errorMessage);

    }

}


查看完整回答
反对 回复 2023-03-02
?
心有法竹

TA贡献1866条经验 获得超5个赞

我建议返回一个 OptionalDouble 对象作为有效或无效的占位符......


Ex #1:  return OptionalDouble.of(num1 + num2); // first if outcome


Ex #2:  return OptionalDouble.empty(); // for the last else

那么您的 main(...) 方法需要类似于...


System.out.println(calc(num1,op,num2).orElseThrow(() -> new IllegalArgumentException("INVALID OPERATOR")));



查看完整回答
反对 回复 2023-03-02
?
大话西游666

TA贡献1817条经验 获得超14个赞

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");

            }


        }


查看完整回答
反对 回复 2023-03-02
  • 3 回答
  • 0 关注
  • 134 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号