-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.java
More file actions
71 lines (52 loc) · 2.02 KB
/
Copy pathTask1.java
File metadata and controls
71 lines (52 loc) · 2.02 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
import java.util.*;
class Task1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char c;
do{
Random r = new Random();
int n = r.nextInt(100) + 1;
int guess = 0;
int count = 0;
boolean won=false;
System.out.println("Welcomeeee to Number Game ");
System.out.println("You have 7 attempts to guess.... Best of Luck ");
do {
if (count == 7) {
System.out.println("Oopss!! Game Over.");
System.out.println("The correct number was: " + n);
break;
}
System.out.print("Attempt " + (count + 1) + " : Enter guess: ");
guess = sc.nextInt();
count++;
if (guess == n) {
System.out.println("Woahhh You guessed correctly!!!");
System.out.println("Attempts used: " + count);
won=true;
}
else if (guess < n) {
System.out.println("Ahh... very low. Try again");
}
else {
System.out.println("Ahh... very high. Try again ");
}
} while (guess != n);
HashMap<Integer,Integer> map=new HashMap<>();
map.put(1,100);
map.put(2,90);
map.put(3,80);
map.put(4,70);
map.put(5,60);
map.put(6,50);
map.put(7,40);
if(won){
System.out.println("Congratulations!! You guessed it right in "+count+" attempts.");
System.out.println("YOUR SCORE= "+map.get(count));
}
System.out.print("Do u want to play again('Y' for Yes, 'N' for No) : ");
c=sc.next().charAt(0);
}while(c=='Y' || c=='y');
System.out.print("Thanks for playing!!");
}
}