简体   繁体   中英

What's wrong with my self-written Calculator.java?

I have written a little calculator in java but it doesn't do what it is supposed to.. :p

So if I enter "a" in the console, it will do the addition as wished. But if I want do another operation, as example multiplication, typing "m" does nothing.

Other operations only seem to work if you go this order in the console (a,s,m,d), probably because that's the order in the code below.

But how can I fix this problem? I want if I type "m" that multiplication is done, or if I type "d" that division is done. What is my mistake here?

import java.util.Scanner;
public class Calculator{
    public static void main(String[] args){
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        Scanner input = new Scanner(System.in);

        System.out.println("a: addition");
        System.out.println("s: subtraction");
        System.out.println("m: multiplication");
        System.out.println("d: division");

        if(input.nextLine().equals("a")){
            double add;
            add=x+y;
            System.out.println(add);
        }
        else if(input.next().equals("s")){
            double sub;
            sub=x-y;
            System.out.println(sub);
        }
        else if(input.next().equals("m")){
            double mul;
            mul=x*y;
            System.out.println(mul);
        }
        else if(input.next().equals("d")){
            double div;
            div=x/y;
            System.out.println(div);
        }
    }
}

Read your input, then do your logic.

import java.util.Scanner;
public class Calculator{
    public static void main(String[] args){
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        Scanner input = new Scanner(System.in);

        System.out.println("a: addition");
        System.out.println("s: subtraction");
        System.out.println("m: multiplication");
        System.out.println("d: division");

        String operator = input.nextLine();

        if(operator.equals("a")){
            double add;
            add=x+y;
            System.out.println(add);
        }
        else if(operator.equals("s")){
            double sub;
            sub=x-y;
            System.out.println(sub);
        }
        else if(operator.equals("m")){
            double mul;
            mul=x*y;
            System.out.println(mul);
        }
        else if(operator.equals("d")){
            double div;
            div=x/y;
            System.out.println(div);
        }
    }
}

The problem you were facing is that each if statement was reading from the input and exhausting the input from System.in . So, it read a line and compared it to "a". If it was "a", it did addition like you said. Otherwise , it went on, trys to read the next character from System.in , and compares that to "s". Etc for "m" and "d".

Rather than read so much input, what you really want is to read the input once ( String operator = input.nextLine() ) and compare that string to your operator strings.

If you want to execute several operations, one after the other one, you should retrieve both the operator and the number from the application and use a loop to allow it instead of providing the numbers as args in your program.
Otherwise you should run your program as much as you need to perform some calculs and it seems undesirable for a Calculator usecase.

Besides, you should avoid mixing input.nextLine() and input.next() , you risk to have some unpredictable results.

import java.util.Scanner;
public class Calculator{

    public static void main(String[] args){         
       Scanner input = new Scanner(System.in);

       while (true){
         System.out.println("a: addition");
         System.out.println("s: subtraction");
         System.out.println("m: multiplication");
         System.out.println("d: division");
         System.out.println("q: quit");

         String operator = input.next();
         if (operator.equals("q"){
           return;
         }

         double x = input.nextDouble();
         double y = input.nextDouble();

        if(operator.equals("a")){
            double add;
            add=x+y;
            System.out.println(add);
        }
        else if(operator.equals("s")){
            double sub;
            sub=x-y;
            System.out.println(sub);
        }
        else if(operator.equals("m")){
            double mul;
            mul=x*y;
            System.out.println(mul);
        }
        else if(operator.equals("d")){
            double div;
            div=x/y;
            System.out.println(div);
        }

         else{
           System.out.println("enter one of the proposed options");
         }

      }
    }
}

Every time you call next() or nextLine(), it would expect input at runtime. So, first, call next() or nextLine() only once per input.

To keep performing operations, you have to run through a loop. You can make it more interactive as follows:

import java.util.Scanner;
public class Calculator{
    public static void main(String[] args){
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);

do{

        System.out.println("a: addition");
        System.out.println("s: subtraction");
        System.out.println("m: multiplication");
        System.out.println("d: division");

    String input = new Scanner(System.in).next();

    if(input.equals("a")){
            double add;
            add=x+y;
            System.out.println(add);
        }
        else if(input.equals("s")){
            double sub;
            sub=x-y;
            System.out.println(sub);
        }
        else if(input.equals("m")){
            double mul;
            mul=x*y;
            System.out.println(mul);
        }
        else if(input.equals("d")){
            double div;
            div=x/y;
            System.out.println(div);
        }

    System.out.print("Want to calculate more (Y/N) : ");
    String choice = new Scanner(System.in).next();
    if(choice.equals("N") || choice.equals("n"))
        break;
    else if(choice.equals("Y") || choice.equals("y"))
        continue;
    else{
            System.out.println("Bad choice");
            break;
    }

}while(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM