public
anonymous / gist:3924199
Created

Number guessing game

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
for(Object o : new Iterable<Object>(){
public Iterator<Object> iterator() {
return new Iterator<Object>(){
 
private static final int MAX_NUMBER = 100;
private static final int MIN_NUMBER = 1;
private Random r = new Random();
private int number;
private int guess;
{
number = r.nextInt(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER;
System.out.printf("guess a number between %d and %d (inclusive).%n",MIN_NUMBER,MAX_NUMBER);
}
private Scanner in = new Scanner(System.in);
public boolean hasNext() {
System.out.print("> ");
guess = in.nextInt();
if(guess == number){
System.out.printf("You win! %d was the right number!%n",number);
return false;
}
System.out.printf("The number is %s than %d%n", number > guess ? "larger" : "smaller",guess);
return true;
}
 
public Object next() {
return null;
}
 
public void remove() {
throw new UnsupportedOperationException();
}
};
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.