Calculator.java 评论和意见

Calculator.java Review And Opinions

这是我最近刚做的一个Java计算器程序,但它不符合我的期望!我想要更方便的方式,比如它有 6 个 类 和一些感叹号,我想获得 A+ 所以请帮助我!

1) 我可以循环代码以便在显示答案后再次运行代码吗? 2) 我能以某种方式减少 类 的数量和代码的长度吗? 3) 我可以像在 C++ 中一样在控制台中清除屏幕,所以它应该为介绍和答案显示一个单独的视图吗?

代码如下:

import java.util.Scanner;
public class javaCalc {

public static void welcome() {
    System.out.println("Welcome to Calculator.java v0.1");
    System.out.println("(Developed By RAZ0229)");
}

public static void main(String[] args) {

    welcome();

    System.out.flush();
    System.out.println("\n1) Addition");
    System.out.println("2) Substraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("\nChoose A Basic Operator:");

    Scanner operandOne = new Scanner(System.in);
    int inpOperation = operandOne.nextInt();

    switch(inpOperation) {
    case 1: additionMethod();
        break;

    case 2: substractionMethod();
        break;

    case 3: multiplicationMethod();
        break;

    case 4: divisionMethod();
        break;

    default: System.out.println("\n(Invalid Argument)");
        return;

    }

}

public static void additionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne + numTwo;
     System.out.println(numOne + " + " + numTwo + " = " + answer);
}

public static void substractionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne - numTwo;
     System.out.println(numOne + " - " + numTwo + " = " + answer);
}

public static void multiplicationMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne * numTwo;
     System.out.println(numOne + " x " + numTwo + " = " + answer);
}

public static void divisionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne / numTwo;
     System.out.println(numOne + " / " + numTwo + " = " + answer);
             }
}

你在每个方法中要求两个浮点数并多次使用相同的打印,所以你可以创建一些这样的方法并在你的操作方法中调用它来停止重复代码(不断重复的代码块是一个强有力的指标,表明该块可能可以抽象为它自己的方法):

public static float[] getValues(){
   float[] values;

   /*Implement your logic here asking user for floats, then put into above array
   and do calculations in your methods using float array*/   

   return values;
}

您还可以通过将主程序包装在一个 while 循环中并在 switch 语句中添加一个额外的 case 来循环您的 main(如果您想退出程序,请输入 5):

public static void main(String[] args) {

  welcome();

  while (true){
    System.out.flush();
    System.out.println("\n1) Addition");
    System.out.println("2) Substraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("5) Quit");
    System.out.println("\nChoose A Basic Operator:");

    Scanner operandOne = new Scanner(System.in);
    int inpOperation = operandOne.nextInt();

    switch(inpOperation) {
      case 1: additionMethod();
        break;

      case 2: substractionMethod();
        break;

      case 3: multiplicationMethod();
        break;

      case 4: divisionMethod();
        break;

      case 5: System.exit(0);

      default: System.out.println("\n(Invalid Argument)");
        return;
    }
  }
}