Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this assignment is to practice using both for and while loops (as

ID: 3682051 • Letter: T

Question

The purpose of this assignment is to practice using both for and while loops (as well as if statements). Write an application called NumberGuesser. Include this import statement: import java.util.Random; In the main method, write Random generator = new Random(); This creates a "random number generator." The method you will use is called nextInt(int n). It picks a random number between 0 and n-1. For example, generator.nextInt(50) gives you a random number between 0 and 49 (i.e., the first 50 integers from 0. Just add 1 if you want 1-50). OK, now create a game in stages: Generate a random number between 1 and 3. Have the user guess the number. If they're right, say so. If they're wrong, say so (tell them if they were too low or too high). Now create a for loop so that this happens 10 times. Next create a variable to keep track of the score. Every time they're right, they gain 5 points. Incorrect, they lose 1 point. Now add in a while loop that makes sure that the entered value is valid. If the user enters in anything outside of the range, it should scold them and make them enter another number Finally make it so that 3 (from step 1) is stored in a (final) variable called TOP. Try changing the value of TOP to 5. Extra-- if that all went easily enough, add in a while loop that lets the person keep guessing until they get each one right.

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class NumberGuesser
   {
       static int score;
       static final int TOP = 3;
       public static void main(String args[])
           {
               int val = 0;
               Random generator = new Random();

               for(int i=0;i<10;i++)
                   {
                       int ran_num = generator.nextInt(TOP)+1;
                       val = guessGame(ran_num);
                   }
               System.out.println("FINAL SCORE : " + val);
           }
          
       public static int guessGame(int num)
           {
               Scanner s = new Scanner(System.in);
               System.out.println("USER: Enter Your guess: Enter a value only between 1 & 3");
               int guess = s.nextInt();
              
               while(guess >=1 && guess <=3)
                   {
                       if(guess == num)
                           {
                               score = score + 5;
                               System.out.println("Your GUESS IS CORRECT. . . !. Number guessed is :" + guess + " Random Number generated is: " + num);
                               break;
                           }
                       else
                           {
                               score = score - 1;
                               System.out.println("Your GUESS IS WRONG. . . !. Number guessed is :" + guess + " Random Number generated is: " + num);
                               break;
                           }              
                   }
               //System.out.println("FINAL SCORE IS: " + score);
               return score;
           }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote