-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadClass.java
More file actions
63 lines (60 loc) · 2.23 KB
/
threadClass.java
File metadata and controls
63 lines (60 loc) · 2.23 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
import java.util.Scanner;
import java.util.Random;
import java.lang.Thread;
public class threadClass {
public static int count=0;
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
Random rd=new Random();
MyRunnable my=new MyRunnable();
Thread thread =new Thread(my);
thread.setDaemon(true); // this tread ends the thread running in background (Myrunnable) as soon as the main thread ends
thread.start();
char c='Y';
while(c=='Y') {
System.out.println("Youe have 20 seconds to write 10 multiple of the # number generated on screeen");
int rand = rd.nextInt(2, 11);
System.out.print("The number is " + rand);
for (int i = 1; i <= 10; i++) {
int multiple = sc.nextInt();
if (multiple % rand != 0) {
System.out.println("wrong multiple!");
System.out.println("you lost!");
System.out.print("Do you want to retake? Y/N : ");
c = sc.next().charAt(0);
switch(c){
case 'Y'->System.out.println("All the best!");
case 'N'->{
c='N';
System.out.println("Thanks for playing");
return;
}
default->System.out.println("invalid input");
}
}
count++; // to count how many correct;
}
}
}
}
class MyRunnable implements Runnable {
@Override
public void run(){
for(int i = 1;i<=20;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread got interrupted");
}
if (i == 10) {
System.out.println("\nTime's up!");
if(threadClass.count<5)
System.out.println("Could have done better!");
else if(threadClass.count>5)
System.out.println("you wrote "+threadClass.count+" multiples!! Not too bad");
System.exit(0); // to exit as sson as the times up!!
}
}
}
}