下面是我用java编写的一些代码,用来使用if语句和扫描。只是寻找一些建议,以改善这个程序或/和教我的技巧,我应该习惯使用。
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
System.out.print("Enter the next value ");
int y = scan.nextInt();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int z = scan.nextInt();
if (z == 1) {
int a = x * y;
System.out.println("Result: " + a);
}//end of if multiply
else if (z == 2) {
int a = x / y;
System.out.println("Result: " + a);
}//end of if divide
else if (z == 3) {
int a = x + y;
System.out.println("Result: " + a);
}// end of if add
else {
int a = x-y;
System.out.println("Result: " + a);
} //end of is subtract
}// End of main
}// End of public class这是我的新程序,仍然不完美,但我所选的课程还没有超过数据类型和类,所以我已经领先了,谢谢大家的帮助!
import java.util.Scanner;
public class Calculatortwo {
public static void main(String[] args) {
//get imput values from user
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
double x = scan.nextDouble();
System.out.print("Enter the next value ");
double y = scan.nextDouble();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int operation = scan.nextInt();
//calculator logic
double result = 0;
switch (operation) {
case 1:
result = x * y;
break;
case 2:
if(y != 0) {
result = x / y;
}
else {
System.out.println("Error: Divide by 0");
}
break;
case 3:
result = x + y;
break;
case 4:
result = x - y;
break;
default:
System.out.println("Error: operation not found");
}
System.out.println("Result: " + result);
}
}发布于 2021-01-20 09:33:39
缺少输入验证。如果用户输入一个字母,程序将在InputMismatchException中失败。
一种方法是捕获异常,输出错误消息,然后退出或要求用户再试一次。
其他案件很少涉及:
y=0,该部门将抛出一个ArithmeticException,因此在发生除法时需要进行第二个验证。1和4之间的整数。if (z == 1) {
...
}//end of if multiply
else if (z == 2) {
...
}//end of if divide
else if (z == 3) {
...
}// end of if add
else {
...
} //end of is subtract在每个子句之后注释// end是不必要的,因为很明显,该子句以一个}结尾。如果注释是为了记住z==1意味着乘法,那么它就是代码不够清晰的标志。可以使用常量或枚举对值的含义进行编码。例如:
private static final int MULTIPLICATION = 1;
//..
if (z == MULTIPLICATION) {
...
}正如@TedBrownlow所指出的,计算器的逻辑可以提取到自己的方法中。这使得代码更易读,更容易测试。通过更容易的测试,我的意思是这样的方法:
public class Calculator {
public static int calculate(int x, int y, int operand) {
//...
}
}可以很容易地在单元测试中进行测试,例如:
public class CalculatorTest {
@Test
public void testMultiplication() {
assertEquals(4, Calculator.calculate(2,2,1));
}
}打印结果会复制四次。通过将a声明在if- end链之外,然后只在末尾打印,可以避免这种情况。发自:
if (z == 1) {
int a = x * y;
System.out.println("Result: " + a);
}//end of if multiply
else if (z == 2) {
int a = x / y;
System.out.println("Result: " + a);
}//end of if divide至:
int a = 0;
if (z == 1) {
a = x * y;
}
else if (z == 2) {
a = x / y;
}
//...
System.out.println("Result: " + a);a和z的一个更好的名称可以是result和choice。
两个整数的除法不一定产生整数。考虑提供更好的准确性,或者通知用户它是整数除法。
发布于 2021-01-20 00:37:21
最好将各种操作编成一个单独的函数。这限制了出错的机会,并消除了System.out.println("Result: " + a);语句的冗余。
private static int applyOperation(int x, int y, int op) {
switch(op) {
case 1: return x*y;
case 2: return x/y;
case 3: return x+y;
case 4: default: return x-y;
}
}
public static void main(String[] args) {
System.out.print("Enter the first value ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
System.out.print("Enter the next value ");
int y = scan.nextInt();
System.out.println("Choose an operation: 1-multiply 2-divide 3-add 4-subtract");
int op = scan.nextInt();
int ans = applyOperation((x), y, op);
System.out.println("Result: " + ans);
}https://codereview.stackexchange.com/questions/254970
复制相似问题