Question: Why won't my java program work? it only outputs -------------------------------------- Output: Operation? (1,2,3,4,5) Input: -------------------------------------- import java.util.Scanner; public class Assignment3{ @SuppressWarnings(resource) public static void main(String

Why won't my java program work?

it only outputs

--------------------------------------

Output: Operation? (1,2,3,4,5)

Input:

--------------------------------------

import java.util.Scanner;

public class Assignment3{

@SuppressWarnings("resource")

public static void main(String args[]){

Scanner in;

in = new Scanner(System.in);

int choice;

double a,b, result, answer;

while(true){

System.out.println("Output: Operation? (1,2,3,4,5) ");

System.out.print("Input: ");

choice = in.nextInt();

if(choice == 1){

// Generate random double

a = Math.random() * 10;

// Generate random double

b = Math.random() * 10;

// Round to the decimals

result = Math.round((a+b)*100)/100.0;

System.out.println("Output: " + a + "+" + b + "?");

System.out.print("Input: ");

answer = in.nextDouble();

if(answer == result)

System.out.println("Output: Correct!");

else

System.out.println("Output: Incorrect! The answer is " + result);

}

else if(choice == 2){

// Generate random double

a = Math.random() * 10;

// Generate random double

b = Math.random() * 10;

// Round to the decimals

result = Math.round((a-b)*100)/100.0;

System.out.println("Output: " + a + "-" + b + "?");

System.out.print("Input: ");

answer = in.nextDouble();

if(answer == result)

System.out.println("Output: Correct!");

else

System.out.println("Output: Incorrect! The answer is " + result);

}

else if(choice == 3){

// Generate random double

a = Math.random() * 10;

// Generate random double

b = Math.random() * 10;

// Round to the decimals

result = Math.round((a*b)*100)/100.0;

System.out.println("Output: " + a + "*" + b + "?");

System.out.print("Input: ");

answer = in.nextDouble();

if(answer == result)

System.out.println("Output: Correct!");

else

System.out.println("Output: Incorrect! The answer is " + result);

}

else if(choice == 4){

a = Math.random() * 10;

b = Math.random() * 10;

result = Math.round((a/b)*100)/100.0;

System.out.println("Output: " + a + "/" + b + "?");

System.out.print("Input: ");

answer = in.nextDouble();

if(answer == result)

System.out.println("Output: Correct!");

else

System.out.println("Output: Incorrect! The answer is " + result);

}

else if(choice == 5){

a = Math.random() * 10;

result = Math.round((Math.pow(2, a))*100)/100.0;

System.out.println("Output: What is 2 to the power of " + a + "?");

System.out.print("Input: ");

answer = in.nextDouble();

if(answer == result)

System.out.println("Output: Correct!");

else

System.out.println("Output: Incorrect! The answer is " + result);

}

else

System.exit(0);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!