-
Notifications
You must be signed in to change notification settings - Fork 0
/
OIBSI task2 : Number guessing
44 lines (39 loc) · 1.21 KB
/
OIBSI task2 : Number guessing
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
import java.util.Random;
import java.util.Scanner;
class guess
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int maxnum ;
int tNumber;
int attempts;
int guess;
maxnum = 100;
tNumber = random.nextInt(maxnum) + 1;
attempts = 0;
guess = 0;
System.out.println("Welcome to Number Guessing Game!");
System.out.println("I chose a number between 1 and " + maxnum + ". Can you guess?");
while (guess != tNumber)
{
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < tNumber)
{
System.out.println("Your guess is too low. Try again!");
}
else if (guess > tNumber)
{
System.out.println("Your guess is too high. Try again!");
} else
{
System.out.println("Congratulations! You guessed correct number " + tNumber );
System.out.println("You took " + attempts + " attempts.");
}
}
scanner.close();
}
}