-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberguessingprogram.java
More file actions
46 lines (35 loc) · 1.61 KB
/
Numberguessingprogram.java
File metadata and controls
46 lines (35 loc) · 1.61 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
import java.util.Random;
import java.util.Scanner;
public class Numberguessingprogram {
public static void main(String[]args) {
/*
write a number that allow an user to guess a number between 1 and 100, keeps the track of attempts made by the
user then when the user guess the number it checks through the number and the set number ( which is randomly generated by the program) for the round when the
number is bigger then the set number the system prints out too high and too low for a lower number
and on correct guess the system prints you won stating the set number and the number of attempts
*/
// number guessing program
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
int min=1;
int max=100;
int randomNumber = random.nextInt(min, max+1);
System.out.printf("This is a number guessing game\n" +
"guess a number between %d-%d\n", min,max);
do {
System.out.print("Enter a gues:");
guess = scanner.nextInt();
if (guess >randomNumber) {
System.out.println("TOO HIGH, TRY AGAIN!>");
} else if (guess < randomNumber) {
System.out.println("TOO LOW, TRY AGAIN!");
} else {
System.out.println("YOU WON, the number is " + randomNumber);
System.out.println("you made " + attempts + " attepts");
}
attempts++;
}while(guess!=randomNumber);
}
}