Hey folks. Beginner Java user and first time on the forum. I have a BS in microbiology from Cal Poly Tech, but after 3 years in the biotech industry, decided to pursue a CS degree. Great opportunity since I can complete in one year through Oregon State's accelerated one year degree since I already have a BS.
Anyway, enough of me, lets get to the good stuff. So, I created a random number game where the user guesses a number (my range is from 1-52) and the cpu tells the user if they are correct, too high, or too low. Simple enough.
For extra credit in my class, (now I won't be angry if this doesn't get solved, but I'm curious to know about how to code this), after a user guesses a number, it updates to the prompt to show a range that the user is wrong, but that they are close to the number. For example:
console displays: pick a number from 1-52. User guesses: 26
console displays: Sorry, that is too low. pick a number from 27-52. User guesses: 40
console displays: Sorry, that number is too high. Pick a number from 27-39
.....
user guesses correctly: 32
console displays: That is correct.
Plus, the user is allowed to play the game repeatedly without having to quit and restart the program.
I understand that this will be an if/else statement, but I'm having a hard time writing the code and telling the JAVA what I want. I also understand that I should use high and low range in the if statements and that the program (if written correctly) will add the half to the low range and/or subtract the half from the high range. Not sure how to display this.
I will display my code with the random number game, if you guys want to critique it. I just need some more guidance for this Extra Credit question. Appreciate any help! Thanks.
import java.util.Scanner; // Taking user import. public class GuessMyCard { public static void main(String[] args) { System.out.println("Welcome to 'Guess The Number!!!!!!'"); // Special instructions for the game. System.out.println(); System.out.println("How to Play:" ); System.out.println("(1) You have 5 guesses to pick a number from 1 to 52!"); System.out.println("(2) If you guess right, you win a big prize!"); System.out.println("(3) If you guess wrong, we'll shoot you out of a cannon!"); System.out.println(); System.out.println("Enter your guess: "); Scanner scanner = new Scanner(System.in); int randomNumber = (int)Math.ceil(Math.random() * 52) + 1; // Math.random helps generate a random integer. Adding 1 makes sure the range is from 1-52 and not starting from 0. int numberofGuesses = 5; // Math.ceil helps round the integer to a whole number. ([url=http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html]Math (Java 2 Platform SE v1.4.2)[/url]). int userGuess = 0; // 5 is set to give the user 5 guesses. while(numberofGuesses > 0) // Loop is declared. { userGuess = scanner.nextInt(); if (userGuess == randomNumber) { // This statement means that if user guesses the random number, then the game is over. System.out.println("YOU HAVE WON USER!!! AND YOUR PRIZE IS THE SATISFACTION OF GUESSING A RANDOM NUMBER CORRECTLY!!!"); break; // Game stops when the user guesses the number correctly. } else if (numberofGuesses == 1) { // Loop will restart if user is wrong, until guesses are up. Make the guess start at 1, not 0. 5 tries are given until the statement is executed. System.out.println("YOU LOSE!!! THE CORRECT ANSWER WAS " + randomNumber + ". TIME FOR CANNON SHOOTING!!!"); } else if (userGuess > randomNumber) { System.out.println("Too high man!"); // If user guesses too high, this statement executes. } else if(userGuess < randomNumber) { System.out.println("Too low man!"); // If user guesses too low, this statement executes. } numberofGuesses--; // Decreases the number of guesses. } } }