-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcersiceOneWeekThree.java
More file actions
75 lines (57 loc) · 1.77 KB
/
Copy pathExcersiceOneWeekThree.java
File metadata and controls
75 lines (57 loc) · 1.77 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package week_3_excersice;
import java.util.Scanner;
import java.util.Random;
public class ExcersiceOneWeekThree {
static Scanner input = new Scanner(System.in);
static Random num = new Random();
public static void main(String[] args) {
System.out.println("Enter a number between 0 - 12(inclusive):");
int n = input();
int next = randomNum();
check(n, next);
playAgain();
// System.out.println(n);
// System.out.println(next);
}
//This method provides other methods with the user's guessed number
public static int input() {
int n = input.nextInt();
return n;
}
//This method generates a random number between 0 - 12
public static int randomNum() {
int next = num.nextInt(13);
return next;
}
//This function checks if the user's guessed number matches the generated random number
public static void check(int n, int next) {
while(n != next) {
if(n > next) {
System.out.println( n + " is too high. Try again!");
}
else if(n < next) {
System.out.println(n + " is too low. Try again!");
}
System.out.println("\nEnter your next Guess: ");
n = input.nextInt();
}
if(n == next) {
System.out.println("Congratulations! You guessed right!");
System.out.println("");
}
}
//This method asks the user if they want to play again
public static void playAgain() {
System.out.println("Do you want to play again? \nEnter Y for Yes and N for No");
String yes = input.next();
if(yes.equalsIgnoreCase("Y")) {
System.out.println("Enter a number between 0 - 12(inclusive):");
int n = input();
int next = randomNum();
check(n, next);
playAgain();
} else if(yes.equalsIgnoreCase("N")){
System.out.println("Thank you for playing. See you next time. Bye");
}
}
}