Last active
January 14, 2024 15:31
-
-
Save PPartisan/5dd32b1e6ddbd5f07a4e1bbcee6bf6bb to your computer and use it in GitHub Desktop.
HiLo.java
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.*; | |
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(); | |
} | |
} |
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.*; | |
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