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