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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started