Skip to content

anonymous /gist:3924199

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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(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();
}
};
}
});
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.