Java 如何缩短此代码并减少重复性?

Java 如何缩短此代码并减少重复性?,java,Java,我做了一个简单的计算器,但是if语句非常重复和冗长。我想知道还有什么其他的解决方案可以缩短它,减少重复性。例如,使用一种方法(我已经尝试过但没有成功)或任何其他可用的技术。最好不要太高,因为我是初学者 import static java.lang.System.*; import static javax.swing.JOptionPane.*; import static java.lang.Integer.*; public class SimpleCalc { public

我做了一个简单的计算器,但是if语句非常重复和冗长。我想知道还有什么其他的解决方案可以缩短它,减少重复性。例如,使用一种方法(我已经尝试过但没有成功)或任何其他可用的技术。最好不要太高,因为我是初学者

import static java.lang.System.*;
import static javax.swing.JOptionPane.*;
import static java.lang.Integer.*;

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

        } else 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));

        } else if (c == 3) {
            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 == 4) {
            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);
在if块之外,因此它只在if块之前询问一次,这将为您节省12行代码


或者,您也可以使用方法或函数作为练习;但这并不会进一步缩短您的代码,真的。我还建议你研究一下Codegolf,你可以学到很多关于代码缩短的知识;你可以移动

      String textA = showInputDialog("Enter first number: ");
      String textB = showInputDialog("Enter second number: ");
      int a = parseInt(textA);
      int b = parseInt(textB);
在if块之外,因此它只在if块之前询问一次,这将为您节省12行代码

或者,您也可以使用方法或函数作为练习;但这并不会进一步缩短您的代码,真的。我还建议你研究一下Codegolf,你可以学到很多关于代码缩短的知识

        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.");
试试像这样的东西

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

有两种方法:

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

在这种情况下,第二种方法效果最好;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);

(我留下了一个问题供您注意和解决……作为学习练习。)

有几种方法:

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

在这种情况下,第二种方法效果最好;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);

(我给你留下了一个问题,让你注意并整理一下……作为一个学习练习。)

以下内容将是相同的,但不会反复重复相同的行。您还可以使用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));
        }
    }
}   

以下内容将是相同的,但不会反复重复相同的行。您还可以使用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));
        }
    }
}   

只是为了好玩。找出常见的东西!并处理需要实现一元运算符的可能性。您可能还希望将其放入一个循环中,并添加一个exit命令

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

只是为了好玩。找出常见的东西!并处理需要实现一元运算符的可能性。您可能还希望将其放入一个循环中,并添加一个exit命令

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

这个问题更适合于开关用例,并遵循S.O.L.I.D。程序。这个问题更适合于开关用例,并遵循S.O.L.I.D。程序。