Last active
August 29, 2015 14:05
-
-
Save stevehenderson/8f76e9b9cc8896e4fa2f to your computer and use it in GitHub Desktop.
Simple java guessing game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//I MADE A CHANGE!! HI EVA | |
package hendo; | |
//We need two packages for reading and handling IO errors | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class JavaGame { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
//a variable for secret question | |
String secretQuestion = "What number am I thinking"; | |
//a variable for secret answer | |
String secretAnswer = "8"; | |
//Bool flag to say when don | |
boolean done = false; | |
//A reader to read keyboard | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
while (! done) { | |
//Display the secret question | |
System.out.println(secretQuestion); | |
String guess = null; | |
//Try reading string from the keyboard -- lots of code for simple task. | |
try { | |
guess = reader.readLine(); | |
} catch (IOException e) { | |
//If there is a problem communicating with keyboard then show the error | |
e.printStackTrace(); | |
} | |
System.out.println("You entered : " + guess); | |
//Did they get it OK? | |
if(guess.equals(secretAnswer)) { | |
System.out.println("CONGRATS! You guessed it!"); | |
done=true; | |
} else { | |
System.out.println("I'm sorry..guess again!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment