1. Engineering
  2. Computer Science
  3. mport javautilrandom mport javautilscanner public class guessinggame t this method...

Question: mport javautilrandom mport javautilscanner public class guessinggame t this method...

Question details

mport java.util.Random; mport java.util.Scanner; public class GuessingGame t This method prompts the user for a guess. It wil only prompt the user ones. The argument provided is the number the user is supposed to guess. : If the user enters a number too high (higher than numberToGuess) then print Too high. If * the user enters a number too low (Lower than numberToGuess) then print Too low. If the * user guesses the number, then print You guessed it. * The number to guess is assumed to be a number between 1 and 29 (but this method doesnt check spp . if numberToGuess provided is indeed between 1 and 20). If the user enters a number that i * not between 1 and 29 (it is less than 1 or greater than 28) then the nes sage to the user will be Guess is not valid and no other message is printed. @param numberToGuess this is the number the user is required to guess. 9 2greturn true if the guess is correct and equal to numberToGuess and false otherwise 4 public static boolean getAndcheckduess(int numberTosuess) t return true 31) 33/ 34 when running this program, it will call getAndCheckGuess with a random number to guess. . If you want to test with a particular number to guess, provide the number to guess * as an argument to this program. For example, if you want the numberToGuess to be 17, * run this program as: java Guessingõane 17 This program will call getAndCheckGuess and then print the value returned by that sp * method/function. 41 param args the command line argument provided. If one is provided it is assumed to be the * number to quess in the game
+ when running this program, it will call getAndCheckGuess with a random number to guess. If you want to test with a particular number to guess, provide the number to guess as an argument to this program. For example, if you want the numberToGuess to be 17, run this program as: java GuessingGame 17 * This program will call getAndCheckGuess and then print the value returned by that t method/function. : eparan args the command line argument provided. If one iT provided it is assumed to be the number to guess in the game. public static void main(String[l args) ( if (args.length < 1) ( Random rand new Random(); // nextInt will return a number between 0 and 20 (exclusive 20). Adding 1 results in // a number between 1 and 20 (inclusive). nunToGuess rand .nextint (20) + 1; , else t // get the number provided as a command line argument and use it as the number to // guess for the game. numToGuess Integer.parseInt (args[e]); boolean correct getAndCheckGuess (numToGuess); System.out.println(correct): 0
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution