This program creates a computer that plays the higher-lower number guessing game with you. The computer picks a number, and you try to guess what it is. Each time you guess, the computer tells you whether the number it’s thinking of is higher or lower than your guess.
import java.util.Scanner;
public class HigherLowerHumanPlayer{
public static void main(String[] args){
int number = (int)(1 + Math.random() * 100);
System.out.println("I'm thinking of a whole number betwen 1 and 100. Guess what it is!");
System.out.println("Please type a whole number and press enter.");
Scanner scanner = new Scanner(System.in);
int guesses = 0;
boolean done = false;
while(!done){
int playerGuess = scanner.nextInt();
guesses++;
if(number < playerGuess){
System.out.println("The number I'm thinking of is lower than " + playerGuess + ".");
System.out.println("Guess again! (Please type a whole number and press enter.)");
}
else if(number > playerGuess){
System.out.println("The number I'm thinking of is greater than " + playerGuess + ".");
System.out.println("Guess again! (Please type a whole number and press enter.)");
}
else{
System.out.println("That's correct! I was thinking of a " + number + ".");
System.out.println("It took you " + guesses + " guesses to get it right. Thanks for playing!");
done = true;
}
}
scanner.close();
}
}
Try to guess what number the computer is thinking.
Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!
Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!