Java - Programming Language
This course covers the basics of programming in Java. Work your way through the videos and we'll teach you everything you need to know to start your programming journey!

Building A Guessing Game

Lesson 20
Author : Mike Dane
Last Updated : October, 2017


Code

Copy// import java.util.Scanner;
Scanner keyboardInput = new Scanner(System.in);

String secretWord = "giraffe";
String guess = "";
int guessCount = 0;
int guessLimit = 3;
boolean outOfGuesses = false;

while(!guess.equals(secretWord) && !outOfGuesses){
     if(guessCount < guessLimit){
          System.out.print("Enter a guess: ");
          guess = keyboardInput.nextLine();
          guessCount++;
     } else {
          outOfGuesses = true;
     }
}

if(outOfGuesses){
     System.out.println("You Lose!");
} else {
     System.out.println("You Win!");
}