-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
30 lines (24 loc) · 852 Bytes
/
GuessingGame.java
File metadata and controls
30 lines (24 loc) · 852 Bytes
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
package com.company;
import java.util.Scanner;
public class GuessingGame {
private int number;
private Range rg;
public GuessingGame(int low, int high) {
this.rg = new Range(low, high);
this.number = new RandomNumber().getRandom(this.rg);
}
public void play(){
boolean guessed = false;
Scanner sc = new Scanner(System.in);
System.out.println("Insert a number within range " + this.rg);
while (!guessed){
guessed = compare(sc.nextInt());
}
System.out.println("Congratulations, you guessed the number!");
}
private boolean compare(int input){
if (input > this.number) System.out.println("Too high! Guess lower");
if (input < this.number) System.out.println("Too low! Guess higher");
return input == this.number;
}
}