-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess_a_number.java
More file actions
61 lines (52 loc) · 1.88 KB
/
Copy pathGuess_a_number.java
File metadata and controls
61 lines (52 loc) · 1.88 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
import java.util.*;
public class Guess_a_number {
public static void main(String args[]) {
int difficulty = 0;
int guess;
int win = 0;
char playagain = 'y';
System.out.println("Welcome to guess a number\n");
do {
System.out.print("Enter a difficulty: ");
Scanner scan = new Scanner(System.in);
difficulty = scan.nextInt();
} while (difficulty == 0);
do {
Random random = new Random();
int rand = random.nextInt(difficulty++);
if (rand == 0) {
continue;
}
System.out.println(rand);
System.out.println("\nyou have 5 guesses");
for (int guesses = 4; guesses != -1; guesses--) {
System.out.print("guess a number 1 - your chosen diffuculty: ");
Scanner scan = new Scanner(System.in);
guess = scan.nextInt();
if (guess == rand) {
win = 1;
break;
}
if (guesses != 0) {
System.out.print("You have ");
System.out.print(guesses);
if (guesses == 1) {
System.out.println(" guess left");
} else {
System.out.println(" guesses left");
}
}
}
if (win == 1) {
System.out.println("You got it!");
} else {
System.out.println("Your guesses ran out!");
}
do {
Scanner scan = new Scanner(System.in);
System.out.print("Do you want to play again? (y/n): ");
playagain = scan.next().charAt(0);
} while (playagain != 'y' && playagain != 'n');
} while (playagain == 'y');
}
}