Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

=+what kinds of policies and practices should be developed?

Answered: 1 week ago

Question

=+ Of the HR issues mentioned in the case,

Answered: 1 week ago