import java.util.*; public class Main { public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); System.out.println("| System.out.println(" System.out.println("|Guessing System.out.println("|Enter System.out.println("Enter System.out.println("] int choice = sc1.nextInt(); sc1.nextLine(); if (choice==1) { Scanner sc = new Scanner(System.in); int num, guess, tries = 0; int max = 100; int min = 1; System.out.println(" System.out.println(" try { jay g num= Integer.parseInt(sc.next()); if (num>0 && num<=100) System.out.println("Enter a number between 1 and 100: "); while(true) { do { guess = (min+max)/2; //(int) break; else System.out.println("Please enter a number between 1 and 100: "); } catch(NumberFormatException ex) { System.out.println("Please enter a number: "); max= guess; } 2 else if(guess < num) { (Math.floor(Math.random()*(max-min+1)+min)); System.out.println("Computer Guessed " +guess); tries++; if(guess > num) { System.out.println("Too High!"); else System.out.println("Too Low!"); min = guess+1; A } 2 Games 1 to play 7 guesses 2 to play Guess the Number"); 1"); do { Welcome to 7 Guesses!"); ‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒---"); System.out.println("\nCorrect! Computer got it in "+tries+ guesses!"); } while(guess != num); } else if (choice==2) { Scanner sc2 = new Scanner(System.in); int num, guess, tries = 0; p MENU int max = 100; _int min = 1; |num = (int) (Math.floor(Math.random( )*(max-min+1)+min)); Guess The Number"); System.out.println(" System.out.println(" ---‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒") P Fors try { Quessa System.out.println("Enter a guess between 1 and 100: "); while(true) { guess-Integer.parseInt(sc2.next()); if(guess>0 && guess<=100)| break; } H H catch (Number FormatException ex) { System.out.println("Please enter a number"); else System.out.println("Please enter a number in the range 1-100: "); } tries++; if(guess > num) { System.out.println("Too High!"); 8 |} ["); MA 1"); } else if (guess < num) { System.out.println("Too Low!"); else } } H System.out.println("Invalid Selection"); 11 System.out.println("\nCorrect! You got it in "+ tries+ " guesses!"); } while (guess != num); } else {

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ
icon
Related questions
Question

Using the Java Code provided in the photo, add comments to the code that explain what the code is doing.

```java
import java.util.*; 

public class Main { 
    public static void main(String[] args) { 
        Scanner sc1 = new Scanner(System.in); 
        System.out.println(" ------------------------------- "); 
        System.out.println(" | MENU |"); 
        System.out.println(" ------------------------------- "); 
        System.out.println(" | Enter 1 to play 7 guesses |"); 
        System.out.println(" | Enter 2 to play Guess the Number| "); 
        System.out.println(" ------------------------------- "); 

        int choice = sc1.nextInt(); 
        sc1.nextLine(); 

        if(choice==1) { 
            Scanner sc = new Scanner(System.in); 
            int num, guess, tries = 0; 
            int max = 100; 
            int min = 1; 
            System.out.println(" Welcome to 7 Guesses! "); 
            System.out.println(" ---------------------- "); 
            System.out.println("Enter a number between 1 and 100: "); 

            while(true) { 
                try { 
                    num = Integer.parseInt(sc.next()); 
                    if (num>0 && num<=100) 
                        break; 
                    else System.out.println("Please enter a number between 1 and 100: "); 
                } 
                catch(NumberFormatException ex) { 
                    System.out.println("Please enter a number: "); 
                } 
            }

            do { 
                guess = (min+max)/2; 
                // int)(Math.floor(Math.random()*(max-min+1)+min)); 
                System.out.println("Computer Guessed " +guess); 
                tries++; 
                if(guess > num) { 
                    System.out.println("Too High!"); 
                    max = guess; 
                } 
                else if(guess < num) { 
                    System.out.println("Too Low!"); 
                    min = guess+1; 
                } 
            } while(guess != num); 
            System.out.println("\nCorrect! Computer got it in "+tries+" guesses!"); 
        }

        else if(choice==2) { 
            Scanner sc2 = new Scanner(System.in); 
            int num, guess, tries = 0; 
            int max = 100; 
            int min = 1; 
            num = (int)(Math.floor(Math.random()*(max-min+1)+min));
Transcribed Image Text:```java import java.util.*; public class Main { public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); System.out.println(" ------------------------------- "); System.out.println(" | MENU |"); System.out.println(" ------------------------------- "); System.out.println(" | Enter 1 to play 7 guesses |"); System.out.println(" | Enter 2 to play Guess the Number| "); System.out.println(" ------------------------------- "); int choice = sc1.nextInt(); sc1.nextLine(); if(choice==1) { Scanner sc = new Scanner(System.in); int num, guess, tries = 0; int max = 100; int min = 1; System.out.println(" Welcome to 7 Guesses! "); System.out.println(" ---------------------- "); System.out.println("Enter a number between 1 and 100: "); while(true) { try { num = Integer.parseInt(sc.next()); if (num>0 && num<=100) break; else System.out.println("Please enter a number between 1 and 100: "); } catch(NumberFormatException ex) { System.out.println("Please enter a number: "); } } do { guess = (min+max)/2; // int)(Math.floor(Math.random()*(max-min+1)+min)); System.out.println("Computer Guessed " +guess); tries++; if(guess > num) { System.out.println("Too High!"); max = guess; } else if(guess < num) { System.out.println("Too Low!"); min = guess+1; } } while(guess != num); System.out.println("\nCorrect! Computer got it in "+tries+" guesses!"); } else if(choice==2) { Scanner sc2 = new Scanner(System.in); int num, guess, tries = 0; int max = 100; int min = 1; num = (int)(Math.floor(Math.random()*(max-min+1)+min));
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY