我的计算器程序的简化编码?

时间:2013-11-07 18:21:05

标签: java

我创建了一个可以进行简单的加法,减法,乘法和除法的计算器。我想知道我的程序是否可以简化,因为我不熟悉Java,我只在线完成了一些教程。这是我的简单计算器的代码:

import java.util.Scanner;
public class Attempt1 {

    public static void main (String []arg){

        double fnum, snum, tnum;
        double answer = 0;
        int turn = 0;
        String operator = "nothing";

        Scanner number = new Scanner(System.in);    
        Scanner Symbol = new Scanner(System.in);

        System.out.println("Enter number");

        if (number.hasNextDouble()){

            fnum = number.nextDouble();

            operator = Symbol.nextLine();

            switch (operator){

            case "+": 
                if (number.hasNextDouble()){

                    snum = number.nextInt();
                    answer = fnum + snum;
                    System.out.println(answer);
                }
                break;
            case "-":
                if (number.hasNextDouble()){

                    snum = number.nextInt();
                    answer = fnum - snum;
                    System.out.println(answer);
                }
                break;
            case "*":
                if (number.hasNextDouble()){

                    snum = number.nextInt();
                    answer = fnum * snum;
                    System.out.println(answer);
                }
                break;
            case "/":
                if (number.hasNextDouble()){

                    snum = number.nextInt();
                    answer = fnum / snum;
                    System.out.println(answer);
                }
                break;
            default:
                System.out.println("You did not enter a number:");
            }

        }else{

            System.out.println("Error");
        }
        do {

            operator = Symbol.nextLine();

            switch (operator){
            case "+": 
                if (number.hasNextDouble()){

                    tnum = number.nextInt();
                    answer = answer + tnum;
                    System.out.println(answer);
                }break;
            case "-":
                if (number.hasNextDouble()){

                    tnum = number.nextInt();
                    answer = answer - tnum;
                    System.out.println(answer);
                }
                break;
            case "*":
                if (number.hasNextDouble()){

                    tnum = number.nextInt();
                    answer = answer * tnum;
                    System.out.println(answer);
                }
                break;
            case "/":
                if (number.hasNextDouble()){

                    tnum = number.nextInt();
                    answer = answer / tnum;
                    System.out.println(answer);
                }
                break;
            default:
                System.out.println("You did not enter a number:");
            }   
        }while (turn < 10);

    }
}

由于

1 个答案:

答案 0 :(得分:1)

是的,它可以。 Java是面向对象的,因此您的代码应该以面向对象的方式设计。对于您的计算器,您有一种方法可以完成所有工作。这是糟糕的风格,因为您不能重复使用重复的代码行。在上面的示例中,每个运算符在两个位置具有相同的代码。相反,你应该把它分解成一个可以处理参数的参数化方法。

public static double operate(String operator, double first, double second){
   switch(operator){
      case "+": return first + second;
      case "-": return first - second;
      //etc...
   }
}

您检索数字的逻辑也应该是一个单独的方法,您可以多次调用以检索数字。例如:

public static double promptNumber(Scanner scan){
    System.out.print("Please enter a number: ");
    if(scan.hasNextDouble())
       return scan.nextDouble();
    else {
       scan.nextLine(); 
       System.out.println("That is not a number");
       return promptNumber(scan);
    }
}

使用这两种辅助方法,您的代码可以大大简化并且更加清晰。祝你好运!