0
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..

4
  • Java? Adding the language tag will get you more answers.
    – brokenfoot
    Apr 7, 2014 at 2:22
  • 1
    what inputs are you using?
    – Reimeus
    Apr 7, 2014 at 2:27
  • 1
    can you provide a sample input and output of what is not working?
    – Mc Kevin
    Apr 7, 2014 at 2:27
  • Try switch (mathOperation.trim().toLowerCase()) and make sure you're using JDK 7 or above. Apr 7, 2014 at 2:51

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.