Calculator.java评论和意见

时间:2018-11-17 07:28:45

标签: java eclipse class calculator cls

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

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

以下是代码:

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

1 个答案:

答案 0 :(得分:0)

您要在每个方法中要求两个浮点数并多次使用相同的打印件,因此您可以创建诸如此类的方法并在您的操作方法中调用它以停止重复代码(不断重复的代码块很指示该块可能可以抽象为自己的方法):

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语句中添加一个额外的大小写来循环主循环(如果您要退出程序,请输入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;
    }
  }
}