如何缩短此代码并减少重复次数?

时间:2018-05-04 22:12:13

标签: java

我制作了一个简单的计算器但if语句非常重复且冗长。我想知道我可以使用什么其他解决方案来缩短它并使其重复性降低。例如,使用方法(我已尝试但未成功)或任何其他可用的技术。因为我是初学者,所以最好不要太先进。

ftp.exe

5 个答案:

答案 0 :(得分:2)

尝试类似

的内容
        String textA = showInputDialog("Enter first number: ");
        String textB = showInputDialog("Enter second number: ");
        int a = parseInt(textA);
        int b = parseInt(textB);
        switch(c) {
        case 1:
            showMessageDialog(null, a + " + " + b + " = " + (a+b));
            break;

        case 2:
        ...
        default:
            showMessageDialog(null, "You cant do that.");

答案 1 :(得分:1)

好吧,开始;你可以移动

      String textA = showInputDialog("Enter first number: ");
      String textB = showInputDialog("Enter second number: ");
      int a = parseInt(textA);
      int b = parseInt(textB);
块之外的

,以便它只在之前 if块请求一次,这样可以节省12行代码。

或者您也可以使用方法或功能作为练习;但是,这不会进一步缩短您的代码。我也建议调查Codegolf,你可以学到很多关于缩短代码的知识。

答案 2 :(得分:1)

有几种方法:

  • 将公共代码放入方法
  • 将公共代码移动到当前方法的不同部分,以便无条件执行。
  • 将非公共代码放入可用于参数化公共代码的函数/方法/类中。

在这种情况下,第二种方法效果最好; e.g。

  if(c==1) {
          String textA = showInputDialog("Enter first number: ");
          String textB = showInputDialog("Enter second number: ");
          int a = parseInt(textA);
          int b = parseInt(textB);
          showMessageDialog(null, a + " + " + b + " = " + (a+b));
  } 
  else if (c==2) {
          String textA = showInputDialog("Enter first number: ");
          String textB = showInputDialog("Enter second number: ");
          int a = parseInt(textA);
          int b = parseInt(textB);
          showMessageDialog(null, a + " - " + b + " = " + (a-b));
  } 
  ...

可以转化为:

  String textA = showInputDialog("Enter first number: ");
  String textB = showInputDialog("Enter second number: ");
  int a = parseInt(textA);
  int b = parseInt(textB);
  int result;
  char op;

  if (c == 1) {
      result = a + b;
      op = '+';
  } else if (c == 2) {
      result = a - b;
      op = '-';
  } 
  ...

  showMessageDialog(null, a + " " + op + " " + b + " = " + result);

(我在那里留下了一个问题让你注意并理清......作为一个学习练习。)

答案 3 :(得分:1)

以下内容相同,但不会反复重复相同的行。您也可以使用switch语句代替4 if / else if语句。

public class SimpleCalc {
    public static void main(String[] args) {
        String operator = showInputDialog("Choose operation: " + "\n" + 
                                          "[1] = Plus" + "\n" +
                                          "[2] = Minus" + "\n" + 
                                          "[3] = Multiply" + "\n" +
                                          "[4] = Divide" + "\n");
        int c = parseInt(operator);
        if (c>4) {
            showMessageDialog(null, "You cant do that.");
            return;
        }
        String textA = showInputDialog("Enter first number: ");
        String textB = showInputDialog("Enter second number: ");
        int a = parseInt(textA);
        int b = parseInt(textB);
        if(c==1) {
            showMessageDialog(null, a + " + " + b + " = " + (a+b));
        } 
        else if (c==2) {
            showMessageDialog(null, a + " - " + b + " = " + (a-b));
        } 
        else if (c==3) {
            showMessageDialog(null, a + " * " + b + " = " + (a*b));
        } 
        else if (c==4) {
            showMessageDialog(null, a + " / " + b + " = " + (a/b));
        }
    }
}   

答案 4 :(得分:0)

只是为了好玩。分解出常见的东西!并处理您需要实施一元运算符的可能性。您可能还想将其置于循环中,并添加退出命令。

public class SimpleCalc {

    public static void main(String[] args) {

        String operator = showInputDialog(
                "Choose operation: " + "\n" + 
                "[1] = Add" + "\n" +
                "[2] = Subtract" + "\n" + 
                "[3] = Multiply" + "\n" +
                "[4] = Divide" + "\n");
                "[5] = Negate" + "\n");

        int c = parseInt(operator);

        int operand_count = 0;
        switch (c) {
            case 1:
            case 2:
            case 3:
            case 4:
                operand_count = 2;
                break;
            case 5:
                operand_count = 1;
                break;
            default:
                showMessageDialog(null, "You cant do that.");
                return(-1);
        }

        int a = 0;
        int b = 0;

        if (operand_count >= 1) {
            String textA = showInputDialog("Enter first number: ");
            int a = parseInt(textA);
        }
        if (operand_count >= 2) {
            String textB = showInputDialog("Enter second number: ");
            int b = parseInt(textB);
        }

        char * opname = "";
        int result = 0;

        switch (c) {
            case 1:
                opname = "+";
                result = a + b;
                break;
            case 2:
                opname = "-";
                result = a - b;
                break;
            case 3:
                opname = "*";
                result = a * b;
                break;
            case 4:
                opname = "/";
                result = a / b;
                break;
            case 5:
                opname = "-";
                result = -a;
                break;
        }

        if (operand_count == 1) {
            showMessageDialog(null, opname + " (" + a + ") = " result);
        } else {
            showMessageDialog(null, a + " " + opname + " " + b + " = " + result);
        }
    }    
}