-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.java
More file actions
45 lines (41 loc) · 1.49 KB
/
task1.java
File metadata and controls
45 lines (41 loc) · 1.49 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
/*NUMBER GAME */
import java.util.*;
import java.util.random.*;
public class task1{
public static void guessNumberGame(){
Scanner sc = new Scanner(System.in);
//for generating the random number
int num = 1+ (int)(100*Math.random());
//give this much chances
int chances = 10;
System.out.println("Hey buddy Lets start the game"
+" A number is choosed between 1 to 100 "
+"Now guess the number within 5 trials."
+"Have fun :) ");
int i =0, guess;
while(i<chances){
System.out.println("Guess the number");
//take input from user for guessing
guess = sc.nextInt();
//if the number is guessed
if(num == guess){
System.out.println("Congratlations! YOU GUESSED THE NUMBER");
break;
}
else if(num>guess && i!= chances -1){
System.out.println("The number is greater than "+guess);
}
else if(num<guess && i!= chances-1){
System.out.println("The number is less than "+guess);
}
i++;
}
if(i == chances){
System.out.println("You are out of chances :( , BETTER LUCK NEXT TIME");
System.out.println("The number was "+num);
}
}
public static void main(String arg[]){
guessNumberGame();
}
}