-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPM_BL_GuessNum.java
More file actions
43 lines (40 loc) · 1009 Bytes
/
PM_BL_GuessNum.java
File metadata and controls
43 lines (40 loc) · 1009 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
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Scanner;
public class PM_BL_GuessNum{
public static void main(String[] args){
runGame(startAndGetNum());
}
public static int startAndGetNum(){
System.out.println("I have a number between 1 and 100.");
System.out.println("Can you guess my number?");
return 1 + (int) (Math.random() * 100);
}
public static void runGame(int num){
Scanner sc = new Scanner(System.in);
int counter = 0;
String single = " try!!!";
String moreThanSingle = " tries!!!";
boolean done = true;
while(done){
int guess = sc.nextInt();
counter++;
if(guess==num){
if(counter ==1)
System.out.println("You got it in " + counter + single);
else
System.out.println("You got it in " + counter + moreThanSingle);
done = !done;
}
else if(guess>num){
System.out.println("Too high!!");
}
else{
System.out.println("Too low!");
}
}
System.out.println("Play again? y/n ");
if(sc.next().equals("y")){
main(new String[1]);
}
sc.close();
}
}