-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGenerator.java
More file actions
80 lines (79 loc) · 2.11 KB
/
RandomGenerator.java
File metadata and controls
80 lines (79 loc) · 2.11 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Random;
import java.util.Scanner;
class Game {
public void player()
{
int roundd=0;
Scanner sc=new Scanner(System.in);
while(true)
{
int totsc=0;
roundd++;
System.out.println();
System.out.println("WELCOME TO NUMBER GUESSING GAME -> ROUND "+roundd);
System.out.println("Enter option 1 for playing\nEnter option 2 for exit game");
int choice=sc.nextInt();
switch(choice)
{
case 1:
{
System.out.println("Enter number of attempts you want for this round");
int flag=sc.nextInt();
while(true)
{
Random ran = new Random();
int min =1;
int max =100;
int r = ran.nextInt(max - min + 1) + min;
System.out.println("Random number generated successfully between 1 to 100\nEnter your guess");
int guess=sc.nextInt();
System.out.println("Random number: " + r);
if(r==guess)
{
System.out.println("correct guess");
totsc=totsc+1;
System.out.println("your score is "+totsc);
}
if(r>guess)
{
int hi=r-guess;
if(hi>20)
{
System.out.println("guess difference is too high");
}
else
System.out.println("you are close...keep trying");
}
if(guess>r)
{
int hi=guess-r;
if(hi>20)
{
System.out.println("guess difference is too high ");
}
else
System.out.println("you are close...keep trying");
}
flag=flag-1;
System.out.println("chance remaining is "+flag);
if(flag==0)
break;
System.out.println();
}
System.out.println("Total score in round " +roundd + " is "+totsc);
System.out.println("\nTHIS ROUND IS OVER...SELECT OPTION FOR PLAYING MORE ROUND OR YOU CAN EXIT ANYTIME");
break;
}
case 2:System.exit(0);
default:System.out.println("Enter valid option");
}
}
}
}
public class RandomGenerator{
public static void main(String args[])
{
Game g1=new Game();
g1.player();
}
}