Java 基于用户输入的结果
我一直在学习enum和switch语句,我有以下代码 我不想通过代码运行参数,然后编译它以查看结果,我希望用户将数字输入到terminal and+,x,/,-中,然后在terminal中获得结果,我该如何实现这一点Java 基于用户输入的结果,java,methods,arguments,calculator,Java,Methods,Arguments,Calculator,我一直在学习enum和switch语句,我有以下代码 我不想通过代码运行参数,然后编译它以查看结果,我希望用户将数字输入到terminal and+,x,/,-中,然后在terminal中获得结果,我该如何实现这一点 public class MathCalculatorTest2_0 { MathFunctions1_0 mathFunction1; public MathCalculatorTest2_0 (MathFunctions1_0 mathFunction1) { this
public class MathCalculatorTest2_0 {
MathFunctions1_0 mathFunction1;
public MathCalculatorTest2_0 (MathFunctions1_0 mathFunction1) {
this.mathFunction1 = mathFunction1;
}
public void DoMath(double digit1, double digit2) {
double mathResult = 0;
switch (mathFunction1) {
case MULTIPLY:
mathResult = digit1*digit2;
break;
case DIVIDE:
mathResult = digit1/digit2;
break;
case ADD:
mathResult = digit1+digit2;
break;
case SUBTRACT:
mathResult = digit1-digit2;
break;
default:
System.out.println("Incorrect arithmatic character, please insert a correct arithmatic character between the two numbers (x, /, +, -)");
}
System.out.println(mathResult);
}
public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Please correct syntax");
} else {
double firstDigit = Double.parseDouble(args[0]);
String userInput1 = args[1];
double secondDigit = Double.parseDouble(args[2]);
MathCalculatorTest2_0 C;
switch (userInput1) {
case "x":
C = new MathCalculatorTest2_0(MathFunctions1_0.MULTIPLY);
C.DoMath(firstDigit, secondDigit);
break;
case "/":
C = new MathCalculatorTest2_0(MathFunctions1_0.DIVIDE);
C.DoMath(firstDigit, secondDigit);
break;
case "+":
C = new MathCalculatorTest2_0(MathFunctions1_0.ADD);
C.DoMath(firstDigit, secondDigit);
break;
case "-":
C = new MathCalculatorTest2_0(MathFunctions1_0.SUBTRACT);
C.DoMath(firstDigit, secondDigit);
break;
default: System.out.println("Please insert correct arithmatic character.");
}
}
}
}首先,您的代码中有很多不好的地方,这可能不是最好的版本,但无论如何它要好得多。使用switch操作符会对您有好处,您可以为需要执行的操作声明一些静态最终变量。 声明将包含一些数学工具的类此静态方法和场:
public class MathTool {
public static final int PLUS_OPERATION = 0;
public static final int MINUS_OPERATION = 1;
public static final int MULTIPLY_OPERATION = 2;
public static final int DIVIDE_OPERATION = 3;
public static double doMathOperation(double digit1, double digit2, int operation) {
switch (operation) {
case PLUS_OPERATION: return digit1 + digit2;
case MINUS_OPERATION: return digit1 - digit2;
case MULTIPLY_OPERATION: return digit1 * digit2;
case DIVIDE_OPERATION: return digit1 / digit2;
default: return 0;
}
}
}
然后您可以像这样调用它的方法:
public static void main(String[] args) {
double operationResult = MathTool.doMathOperation(5, 3, MathTool.MULTIPLY_OPERATION);
System.out.println(operationResult);
}
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
在Java中,应该使用小写字母作为变量名的第一个字母
编辑:
对于来自控制台的用户输入,请使用如下扫描仪:
public static void main(String[] args) {
double operationResult = MathTool.doMathOperation(5, 3, MathTool.MULTIPLY_OPERATION);
System.out.println(operationResult);
}
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
首先看一下这个:第二:不要以大写字母开始命名变量。第三:给出有意义的名称(这里的类名/实例名)无论如何,从方法、设计等方面来说,这有很多不好的地方。您好,谢谢您的评论,但是我对Java非常陌生,我不是计算机科学专业的学生,我从来没有上过课。。。所以我还不知道如何更好地设计任何东西。。。请不要这么挑剔。另外,这如何帮助我让用户输入10x10并得到100?您好,谢谢您的回复,但是我如何从命令行获得用户输入?我对Java真的很陌生,我知道的不多,这是我写的第一个应用。。。那么,用户如何才能输入数字来执行某些数学操作,如乘法10x10???Yuri-枚举在几乎所有情况下都优于公共静态最终整数在这种情况下是否使用枚举取决于您。我更喜欢仅当枚举中有一些逻辑时才使用枚举。