IT/Programming / / 2023. 4. 20. 13:15

[JAVA] 구구단 문제 시간경과 같이 출력 예제

반응형
package dayday17;

import javax.swing.JOptionPane;

/*
 * 1.구구단 5문제를 푸는데 몇초가 소요되었는지 출력
 *  흐름1. 구구단 5문제 내고 맞춘다.
 *  흐름2. 초세기 
 * 
 */

class timers extends Thread {
	
	int i = 0;
	synchronized public void run(){ //synchronized 동기화
		try {
			while(true){
				Thread.sleep(1000);
				i++;
			}
		} catch (InterruptedException e) {}
	}
}

public class Quiz01 {
	public static void main(String[] args) {
		timers t = new timers();
		t.setDaemon(true);
		t.start();
		int stack = 1;
		int ok = 0;
		int fault = 0;
		while (stack != 6) {
			int result = 0;
			int i = (int) (Math.random() * 10);
			int j = (int) (Math.random() * 10);
			String mess = JOptionPane.showInputDialog(stack+"번째 문제!\n"+i + " * " + j + " = ?");
			result = Integer.parseInt(mess);
			if((i*j) == result){
				ok++;
			}else{
				fault ++;
			}
			stack++;
		}
		System.out.println(t.i+"초 소요\n"+ok+"번 정답\n"+fault+"번 틀렸습니다.");
	}
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유