首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仍然在学习,寻找关于我用java编程的4功能计算器的一般建议。

仍然在学习,寻找关于我用java编程的4功能计算器的一般建议。
EN

Code Review用户
提问于 2021-01-19 23:05:25
回答 2查看 79关注 0票数 2

下面是我用java编写的一些代码,用来使用if语句和扫描。只是寻找一些建议,以改善这个程序或/和教我的技巧,我应该习惯使用。

代码语言:javascript
复制
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

这是我的新程序,仍然不完美,但我所选的课程还没有超过数据类型和类,所以我已经领先了,谢谢大家的帮助!

代码语言:javascript
复制
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);
   }
}
EN

回答 2

Code Review用户

回答已采纳

发布于 2021-01-20 09:33:39

输入验证

缺少输入验证。如果用户输入一个字母,程序将在InputMismatchException中失败。

一种方法是捕获异常,输出错误消息,然后退出或要求用户再试一次。

其他案件很少涉及:

  • 对于y=0,该部门将抛出一个ArithmeticException,因此在发生除法时需要进行第二个验证。
  • 操作的选择只能是14之间的整数。
  • 对两个大整数的操作可以使整数结果溢出。

不必要的注释

代码语言:javascript
复制
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意味着乘法,那么它就是代码不够清晰的标志。可以使用常量或枚举对值的含义进行编码。例如:

代码语言:javascript
复制
private static final int MULTIPLICATION = 1;
//..
if (z == MULTIPLICATION) {
        ...
}

提取法

正如@TedBrownlow所指出的,计算器的逻辑可以提取到自己的方法中。这使得代码更易读,更容易测试。通过更容易的测试,我的意思是这样的方法:

代码语言:javascript
复制
public class Calculator {
   public static int calculate(int x, int y, int operand) {
       //...
   }
}

可以很容易地在单元测试中进行测试,例如:

代码语言:javascript
复制
public class CalculatorTest {
   @Test
   public void testMultiplication() {
       assertEquals(4, Calculator.calculate(2,2,1));
   }
}

重复代码

打印结果会复制四次。通过将a声明在if- end链之外,然后只在末尾打印,可以避免这种情况。发自:

代码语言:javascript
复制
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

至:

代码语言:javascript
复制
int a = 0;
if (z == 1) {
    a = x * y;
}
else if (z == 2) {
    a = x / y;
}
//...
System.out.println("Result: " + a);

命名

az的一个更好的名称可以是resultchoice

精度

两个整数的除法不一定产生整数。考虑提供更好的准确性,或者通知用户它是整数除法。

票数 3
EN

Code Review用户

发布于 2021-01-20 00:37:21

最好将各种操作编成一个单独的函数。这限制了出错的机会,并消除了System.out.println("Result: " + a);语句的冗余。

代码语言:javascript
复制
   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);
   }
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/254970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档