-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneShotHiLo.java
More file actions
49 lines (39 loc) · 1.53 KB
/
OneShotHiLo.java
File metadata and controls
49 lines (39 loc) · 1.53 KB
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
43
44
45
46
47
48
49
/*
https://programmingbydoing.com/a/one-shot-hi-lo.html
Write a program that picks a random number from 1-100.
Give the user a chance to guess it. If they get it right, tell them so.
If their guess is higher than the number, say "Too high."
If their guess is lower than the number, say "Too low." Then quit. (They don't get any more guesses for now.)
How much wood would a wood chuck chuck if a wood chuck could chuck wood?
*/
import java.util.*;
public class OneShotHiLo {
public static void generateRandom(int guess, int randomRange){
Random roll = new Random();
int rolly = roll.nextInt(randomRange) + 1;
if(guess > rolly){
System.out.println("Sorry too high! I was thinking of " + rolly);
}
else if(guess < rolly){
System.out.println("Sorry too low! I was thinking of " + rolly);
}
else if(guess == rolly){
System.out.println("You guessed it! What are the odds of guessing " + rolly);
}
else{
System.out.println("You broke this somehow!");
}
}
public static void main(String[] args) {
int guess;
int randomRange = 100;
int numberOfTries = 10;
Scanner keyboard = new Scanner(System.in);
System.out.println("I'm thinking of a number between 1-00. Try to guess it!");
guess = keyboard.nextInt();
// modified to run method multiple times for testing
for(int i = 0; i < numberOfTries; ++i){
generateRandom(guess, randomRange);
}
}
}