-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
39 lines (38 loc) · 1.29 KB
/
NumberGame.java
File metadata and controls
39 lines (38 loc) · 1.29 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
import java.util.*;
public class NumberGame {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Random random=new Random();
int score=5;
int count=1;
System.out.println(" --> Welcome to Number Game <--");
System.out.print("Enter Your Name :");
String name=sc.nextLine();
System.out.println(name +" You will Be having 5 Chances to guess the number");
int randomnum=random.nextInt(100)+1;
int user=0;
for (int i=0;i<5;i++){
System.out.print("ENTER A NUMBER :");
user=sc.nextInt();
if(user==randomnum){
System.out.println("Congratulations "+name+ " have matched the random number "+count+" attempts");
System.out.println("Your score is "+score);
break;
}
else if(user<randomnum){
System.out.println(name+ " your number is less than random number !");
score=score-1;
count+=1;
}
else{
System.out.println(name+ " your number is high than random number !");
score=score-1;
count+=1;
}
}
if(user!=randomnum){
System.out.println("Chances over for the Number Game ");
System.out.println("Try Again ");
}
}
}