【问题标题】:How can I write a function that returns the mathematical operator if two variables are equal to the third variable? [closed]如果两个变量等于第三个变量,我如何编写一个返回数学运算符的函数? [关闭]
【发布时间】:2021-08-10 07:44:46
【问题描述】:

例如

public String mathFunction(4,2,6) returns "+",
public String mathFunction(2,2,4) returns "+*", (+ and *)
public String mathFunction(7,1,12) returns "None"

/* 我的代码 */

public class mathFunction {
    public static void main(String... nums) {
        int num1, num2, num3;
        if (num1 * num2 == num3) { /*returns "*" if true */ 
            String multiply = public static String.valueOf(nums);
            System.out.println(multiply);
        }
        else if (num1 / num2 == num3) { /*returns "/" if true */ 
            String divide = public static String.valueOf(nums);
            System.out.println(divide);
        }
        else if (num1 + num2 == num3) {  /*returns "+" if true */                             
            String add = public static String.valueOf(nums);
            System.out.println(add);
        }
        else if (num1 - num2 == num3) { /*returns "-" if true */
            String subtract = public static String.valueOf(nums);
            System.out.println(subtract);
        }
        else {
            System.out.println("None")
        } 
    }
}

【问题讨论】:

  • 您的问题到底是什么?
  • 方法体内public static的用途是什么?
  • 这个String multiply = public static String.valueOf(nums); 没有任何意义。如果要打印*符号,可以System.out.print('*');
  • 您的方法接受一个名为 nums 的变量,但您正在尝试使用名为 num1num2num3 的变量,这些变量没有声明值。
  • 您的指令还说您需要一个名为mathFunction方法,该方法返回一个字符串。您的代码中没有任何迹象表明您尝试编写这样的方法。

标签: java function if-statement parameter-passing return-value


【解决方案1】:

首先,如果你想让你的函数识别多个运算符,比如public String mathFunction(2,2,4) returns "+*", (+ and *),你不应该使用else if

因为如果ifs 之一为真,则不会检查下一个else ifs。

在您的情况下,所有条件都必须写为if,如果其中任何一个为真,则该条件的运算符必须添加到operators 字符串中。

最后,你必须检查如果operators为空,函数返回"None",否则返回operators

public String mathFunction(int num1, int num2, int num3){
    String operators = "";
    if(num1 + num2 == num3){
        operators += "+";
    }
    if(num1 - num2 == num3){
        operators += "-";
    }
    if(num1 * num2 == num3){
        operators += "*";
    }
    if(num1 / num2 == num3){
        operators += "/";
    }
    if(operators.equals(""){
        return "None";
    } else{
        return operators;
    }
}

【讨论】:

    【解决方案2】:

    我会做这样的事情(如果 Java Synthax 不正确,请见谅):

        public string mathFunction(double num1, double num2, double num3)
        {
            string result = string.Empty;
            if (num1 * num2 == num3)
            {
                result += "*";
            }
            if (num1 / num2 == num3)
            {
                result += "/";
            }
            if (num1 + num2 == num3)
            {
                result += "+";
            }
            if (num1 - num2 == num3)
            {
                result += "-";
            }
            if (string.IsNullOrWhiteSpace(result)) return "None";
    
            return result;
        }
    

    【讨论】:

      【解决方案3】:

      这里的主要问题是您反复使用else if。这意味着其中一个表达式为真,其他表达式永远不会被检查。

      我建议使用 StringJoiner(或者只使用良好的旧字符串连接)。它的最基本版本是这样的

        public static void main(String... nums) {
              int num1 = Integer.valueOf(nums[0]);
              int num2 = Integer.valueOf(nums[1]);
              int num3 = Integer.valueOf(nums[2]);
      
              StringBuilder output = new StringBuilder();
              if (num1 * num2 == num3) { /*returns "*" if true */ 
                  output.append("*");
              }
              if (num1 / num2 == num3) { /*returns "/" if true */ 
                  output.append("/");
              }
              if (num1 + num2 == num3) {  /*returns "+" if true */                             
                  output.append("+");
              }
              if (num1 - num2 == num3) { /*returns "-" if true */
                  output.append("-");
              }
              if (output.length() == 0) {
                  System.out.println("None");
              } else {
                  System.out.println(output.toString());
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-31
        • 1970-01-01
        • 2020-02-29
        • 2019-04-13
        • 2021-05-13
        • 2021-05-03
        • 1970-01-01
        • 2018-03-26
        • 2021-05-15
        相关资源
        最近更新 更多