简体   繁体   中英

Default statement for my switch statement is not executing?

import java.util.Random; //Needed for Random Class
import java.util.Scanner; //Needed for Scanner Class

public class Project3 
{

    public static void main(String[] args) 
    {
        //Declare Variables
        double num1;
        double num2;
        int addition;
        double division;
        int multiplication;
        int subtraction;
        int modulus;
        String mathOperation; //To get what type of problem user wants to do

        //Create Scanner Object for kb input
        Scanner kb = new Scanner(System.in);

        //Create Random Object
        Random randomNumbers = new Random();

        //Get randomly generated numbers
        num1 = randomNumbers.nextInt(10);
        num2 = randomNumbers.nextInt(10);

        //display randomly generated numbers for user
        System.out.println("The numbers are " + num1 + " and " + num2);

        //ask user which operation they want to do
        System.out.println("Which arithmetic operation do you wish to use?"
                + " We can do addition, subtraction, multiplication, division, and modulus.");
        mathOperation = kb.nextLine();

        double guess=0;
        System.out.println("What do you think the answer will be?");
        guess = kb.nextDouble();

        //Determine which arithmetic operation to be checked

        switch (mathOperation)  
        {
        case "addition":

            System.out.println("The answer is: " + (num1 + num2));

            if (guess == num1 + num2)
                System.out.println("You are correct!");
            else
                System.out.println("You are wrong.");
            break;


        case "subtraction":

            System.out.println("The answer is: " + (num1 - num2));

            if (guess == num1 - num2)
                System.out.println("You are correct!");
            else
                System.out.println("You are wrong.");
            break;

        case "multiplication":

            System.out.println("The answer is: " + (num1*num2));

            if (guess == num1 * num2)
                System.out.println("You are correct!");
            else
                System.out.println("You are wrong.");
            break;
            //division - use WHILE selection statement
        case "division":

            while (num2 == 0)
            {
                System.out.println("Cannot determine answer since the divisor equals zero.");
                num2++;
                System.exit(1);
            } 


            System.out.println( "The answer is: " + (num1/num2));

            if (guess == num1 / num2)
                System.out.println("You are correct!");
            else
                System.out.println("You are wrong.");
            break;

        case "modulus":

            System.out.println( "The answer is: " + (num1 % num2));

            if (guess == num1 % num2)
                System.out.println("You are correct!");
            else
                System.out.println("You are wrong.");
            break;

        default:
                System.out.println("Invalid choice.");

    }

    }
}

All of the code works, but I can't seem to get the default statement to work properly.. not sure if there is something wrong in the code the prevents it from working or there is just something wrong with the default itself. Although, I've looked and looked, and it all seems right. Pretty lost..

Look at this to know why can't switch on a string(depends on the version you are using btw): Why can't I switch on a String?

jdk 7 example:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

My two cents: Though adding break to the default is not mandatory, it's a good practice to do so as it helps when you are making changes to the order of the case statements

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