Skip to content
Create a gist now

Instantly share code, notes, and snippets.

anonymous /gist:3924199
Created Oct 20, 2012

Number guessing game
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(;
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.