Question

I posted this earlier but the answer did not provide three methods. Can you please post the answer with three methods. Also if possible can it be in C#. Java is also fine but please have three methods in the code. Also the main method. Exercise #3 Guess the Number Write a Guess the Number game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier You can write this a number of ways,but try to stick to the instructions outlined here. Write three methods to make your game work . The first method should generate a random number and return the value to main to store in the secret number variable. The second method should ask your user what his first guess is. Return this information to main. The third method should take in the guess and the secret number via parameters, then evaluate whether or not the guess is correct. Return a string to let the user know if their guess was too high, too low, or correct . You will need a nested loop in main. Youll want the outer loop to ask if the user wants to play again. Youll also need to think about how to include an inner loop that continues to call the third method until the user guesses correctly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA

////////////////////////////////// CODE TO COPY /////////////////////////////////////

import java.util.Random;

import java.util.Scanner;

public class GuessNumber {

public static void main(String[] args) {

int rerun = 1; // Initializing required variables

int guess;

int reguess = 0;

int random;

int noOfTimes=1;

String result;

String replay;

Scanner scan = new Scanner(System.in); // Initializing scanner to read input

for(int i = 0; i < rerun ; i++ ) { // Outer Loop for Guessing new random number again

random = getRandomNumber(); // calling method to generate the random number

System.out.println("Random-->"+random); // *You can omit this step. I have added this step to show you in output that the code works correctly*

guess = getGuess(); // Calling the method to get the guessed number from user

result = validateGuess(random,guess); //Calling the method to validate the number

for(int j=0; j < noOfTimes; j++) {

// Inner loop for getting guesses again for same number if previous guess is wrong

if(j>0) {

// Validating the second and consecutive guesses

result = validateGuess(random,reguess);  

}

if(result.equalsIgnoreCase("Correct")) {

System.out.println("Guess is "+result+"\n");

break; // Breaking the inner loop if guess is correct

}

else if(result.equalsIgnoreCase("Too High") || result.equalsIgnoreCase("Too Low")) {

System.out.println("Guess is "+result);

reguess = getGuess(); // Getting the consecutive Guesses

noOfTimes++;

// Incrementing the noOfTimes variable to continue the inner loop

}

}

System.out.println("Want to Play again? Enter Yes or No");

replay = scan.next(); // Getting the value for replay

if(replay.equalsIgnoreCase("Yes")) {

rerun++;   // Incrementing the rerun variable to rerun the guess if user enters yes

}

else if(replay.equalsIgnoreCase("No")){

System.out.println("End");

break; // Breaking the Outer loop to end the program if user enters No

}

}

}

/*

* FIRST METHOD : Generating Random Number returns to main

*/

public static int getRandomNumber() {

Random r = new Random();

int rand = r.nextInt((20-1)+1)+1; // Format for random numbers between 1 and 20

// r.nextInt(( maximumValue - MinimumValue ) + 1 ) + MinimumValue;

return rand;   

}

/*

* SECOND MEHTOD : Getting the Guess from user and returns to main

*/

public static int getGuess() {

Scanner scan = new Scanner(System.in);

System.out.println("Guess the number");

return scan.nextInt();

}

/*

* THIRD METHOD : Validating the guess and returns the string result to main

*/

public static String validateGuess(int random, int guess) {

String result=null;

if(guess > random) {

result = "Too High";

}

else if(guess < random) {

result = "Too Low";

}

else if(guess == random) {

result = "Correct";

}

return result;

}

}

////////////////////////////////////////// OUTPUT ///////////////////////////////////////////

R Markers properties Servers膼Data Sou terminated> GuessNumber [Java Application] C: Pro Random--12 Guess the number Guess is Too Low Guess the number 15 Guess is Too High Guess the number 12 Guess is Correct Want to Play again? Enter Yes or No yes Random--11 Guess the number 19 Guess is Too High Guess the number 17 Guess is Too High Guess the number Guess is Correct Want to Play again? Enter Yes or No No End

Add a comment
Know the answer?
Add Answer to:
I posted this earlier but the answer did not provide three methods. Can you please post...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...

    I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 13720, and the user guesses 83521, the...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Code in JAVA.................... Guess the Number In this activity, you will write a REST server to...

    Code in JAVA.................... Guess the Number In this activity, you will write a REST server to facilitate playing a number guessing game known as "Bulls and Cows". In each game, a 4-digit number is generated where every digit is different. For each round, the user guesses a number and is told the exact and partial digit matches. An exact match occurs when the user guesses the correct digit in the correct position. A partial match occurs when the user guesses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT