我的switch语句的默认语句没有执行?

时间:2014-04-07 02:19:20

标签: java switch-statement

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

    }

    }
}

所有代码都可以工作,但我似乎无法使默认语句正常工作..不确定代码中是否存在错误,导致其无法正常工作或默认情况下出现问题。虽然,我看起来和看起来,这似乎都是正确的。很丢失..

1 个答案:

答案 0 :(得分:0)

看看这个就知道为什么不能打开一个字符串(取决于你使用btw的版本): Why can't I switch on a String?

jdk 7例子:

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

我的两分钱:尽管在默认情况下添加中断并不是强制性的,但这样做是一种很好的做法,因为当您更改案例陈述的顺序时它会有所帮助