1

I created a calculator that can do simple addition, subtraction, multiplication and division. I want to know if my program can get simplified bearing in mind that I am not that experienced with Java, I only completed a few tutorials online. Here is my code for my simple calculator:

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);

    }
}

Thanks

4

1 回答 1

1

Yes it can. Java is object oriented and as such your code should be designed in an object oriented fashion. In the case of your calculator you have one method that does all the work. This is bad style because you can't reuse repeated lines of code. In your above example, you have the same code in two locations for every operator. Instead you should break that up into a parameterized method that can handle the arguments.

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

Your logic for retrieving the the numbers should also be a separate method that you can call as many times to retrieve a number. For example:

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);
    }
}

Using these two helper methods your code can be greatly simplified and much cleaner. Good luck!

于 2013-11-07T18:35:58.103 回答