简体   繁体   中英

Making a calculator using switch case

The result i get :

Enter a number : 
5
Enter another number : 
4
What do you want to perform on these numbers? 
You have entered a wrong action, please try again 

Where did i go wrong in my code?

import java.util.Scanner;

    public class App {

        public static void main(String[] args) {

            double num1, num2;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a number : ");
            num1 = sc.nextDouble();
            System.out.println("Enter another number : ");
            num2 = sc.nextDouble();

            System.out.println("What do you want to perform on these numbers? ");
            String word = sc.nextLine();

            sc.close();

            double result = 0;
            switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

            }

        }

    }

Can you change your code like below:

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine(); // ADD THIS LINE
        String word = sc.nextLine();

The problem here is with the num2 = sc.nextDouble(); the newline char is not consumed.

Below is the code I use:

public static void main(String args[]) throws Exception {
      //  A1Pattern.printPattern(26);


        double num1, num2;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        num1 = sc.nextDouble();
        System.out.println("Enter another number : ");
        num2 = sc.nextDouble();

        System.out.println("What do you want to perform on these numbers? ");
        sc.nextLine();
        String word = sc.nextLine();

        sc.close();

        double result = 0;
        switch (word) {
            case "Addition":
                result = num1 + num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Subtraction":
                result = num1 - num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Multiplication":
                result = num1 * num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            case "Division":
                result = num1 / num2;
                System.out.println(num1 + " " + word + " " + num2 + " : " + result);
                break;
            default:
                System.out.println("You have entered a wrong action, please try again ");
                break;

        }

    }
  • OUTPUT:

    Enter a number : 1 Enter another number : 2 What do you want to perform on these numbers? Subtraction 1.0 Subtraction 2.0 : -1.0

Instead of using sc.nextline() on line 15 use sc.next() . The program will now wait for your input before continuing.

The java.util.Scanner.next() method finds and returns the next complete token from this scanner.

Use scanner.next() instead of scanner.nextLine().

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