Skip to content

Instantly share code, notes, and snippets.

@PPartisan
Last active January 14, 2024 15:31
Show Gist options
  • Save PPartisan/5dd32b1e6ddbd5f07a4e1bbcee6bf6bb to your computer and use it in GitHub Desktop.
Save PPartisan/5dd32b1e6ddbd5f07a4e1bbcee6bf6bb to your computer and use it in GitHub Desktop.
HiLo.java
import java.util.*;
public class HiLo {
int random;
int guess;
public void generateNumber() {
random = (int)(Math.random()*100)+1;
}
public void guess() {
final Scanner sc=new Scanner(System.in);
do {
System.out.println("Enter your guess:");
guess = sc.nextInt();
if(guess < random) {
System.out.println("Low");
} else if (guess > random){
System.out.println("High");
}
} while(guess != random);
System.out.println("Hit");
}
void startGuessing() {
guess();
}
}
import java.util.*;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class HiLo {
int random;
void generateNumber() {
random = (int)(Math.random()*100)+1;
}
void startGuessing() {
try(final Scanner in = new Scanner(System.in)) {
startGuessing(in);
}
System.out.println("Hit.");
}
private void startGuessing(Scanner in) {
IntStream.generate(takeGuessFrom(in))
.takeWhile(i -> i != random)
.mapToObj(i -> (i > random) ? "High" : "Low")
.forEach(System.out::println);
}
private IntSupplier takeGuessFrom(Scanner in) {
return () -> {
System.out.println("Enter your guess:");
return in.nextInt();
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment