0

这是我最近刚做的一个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);
             }
}
4

1 回答 1

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

您还可以通过将 main 包装在一个 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;
    }
  }
}
于 2018-11-17T07:38:54.860 回答