Answer to Question #306879 in Java | JSP | JSF for skillie

Question #306879

Write Java program that randomly generate two Integers between 1 and 10 as input and perform an operation (+,-,*,x,/) on them and displays the result of that operation. The program should tell the user whether the result is an even number or odd number. If the result is an even number the program should generate again two integers and repeat the process again. The program will only stop when the result of the operation is an odd number.

Hint: (An even number is a number that can be divided into two equal groups. An odd number is a number that cannot be divided into two equal groups)

Expected output of the program

The number of time the program generated the random number.

The value of the odd that stops the program.

Marks allocation

1. Use of a random function - 10 Marks

2. Use of a loop - 10 Marks

3. Use of a switch case 10 Marks

4. The use of a sentinel value - Odd number to stop the program - 10

4. correct output- 10


1
Expert's answer
2022-03-06T18:18:13-0500
import java.util.*;
import java.math.*;


public class Main {
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        int result = 1;
        int NumberOfGenerations = 0;
        
        System.out.println("Enter operation: +,-,*,x,/");
        String operation = in.nextLine();
        
        do{
          
          NumberOfGenerations++;
          int one = (int)(Math.random() * 10) + 1;
          int two = (int)(Math.random() * 10) + 1;
          
          switch(operation){
            case ("+"):
            result = one + two;
            break;
            
            case("-"):
            result = one - two;
            break;
            
            case("*"):
            result = one * two;
            break;
            
            case("x"):
            result = one * two;
            break;
              
            case("/"):
            result = one / two;
            break;
            
            default:
            System.out.println("Try again - value does not match: +,-,*,x,/");
            operation = in.nextLine();
            break;
          }
        
        }while(result%2 == 0);
          
      
        System.out.printf("Number of generations: %d \n",NumberOfGenerations);
        System.out.printf("Value of the odd: %d \n",result);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS