임의의 난수(1~10)를 두개 더해서 맞추는 프로그램
제한시간은 60초
1. 사용할 클래스
Random, Scanner
2.큰 틀 작성 : 인터페이스 구현 클래스, main메소드
class SumThread implements Runnable{
@Override
public void run(){
}
}
class TimeAttackThread implements Runnable{
@Override
public void run(){
}
}
public class Test{
public static void main(String[]args){
new Thread(new SumThread()).start();
new Thread(new TimeAttackThread()).start();
}
}
3. 구현부 작성
class SumThread implements Runnable{
Static int cnt;
@Override
public void run(){
while(true){
Random r = new Random();
int a = r.nextInt(10)+1;
int b = r.nextInt(10)+1;
System.out.println(a+"+"+b+"=");
int c = sc.nextInt();
if(c==(a+b)){
cnt++;
System.out.println("정답입니다. 맞춘개수: "+cnt);
}else{
System.out.println("틀렸습니다. 맞춘개수: "+cnt);
}
}
}
class TimeAttackThread implements Runnable{
@Override
public void run(){
int count=0;
while(true){
count++;
if(count==60){
System.out.println("덧셈이 종료되었습니다.");
System.out.println("맞춘개수: "+SumThread.cnt);
System.exit(0);
}else if(count%10==0){
System.out.println((60-count)+"초 남았습니다.");
}
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.out.println(ie.getMessage());
}
}
}
public class Test{
public static void main(String[]args){
new Thread(new SumThread()).start();
new Thread(new TimeAttackThread()).start();
}
}