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