Created
February 21, 2014 05:47
-
-
Save ltagliaferri/9129446 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.Scanner; | |
import java.util.Random; | |
public class MagicNumber { | |
private static void playTheGame(int magicNumber) { | |
int numberOfTries = 0; | |
Scanner input = new Scanner(System.in); | |
int guess; | |
boolean win = false; | |
while (win == false) { | |
System.out.println("Guess a number between 1 and 100: "); | |
guess = input.nextInt(); | |
numberOfTries++; | |
if (guess == magicNumber) { | |
win = true; | |
} | |
else if (guess < magicNumber){ | |
System.out.println("Too low!"); | |
} | |
else if (guess > magicNumber) { | |
System.out.println("Too high!"); | |
} | |
} | |
System.out.println("Just right!"); | |
System.out.println("The number was " + magicNumber +"."); | |
System.out.println("It took you " + numberOfTries + " guesses."); | |
} | |
private static int createMagicNumber() { | |
Random rand = new Random (); | |
int magicNumber = rand.nextInt(101) + 1; | |
return magicNumber; | |
} | |
public static void main(String[] args) { | |
int magicNumber = createMagicNumber(); | |
playTheGame(magicNumber); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment