import java.util.Calendar;
import java.util.Scanner;
public class ex6 {
public static int Second(){
Calendar cal = Calendar.getInstance();
int sec = cal.get(Calendar.SECOND);
return sec;
}
void run() {
Scanner sc = new Scanner(System.in);
String enter;
String name[] = {"황기태","이기문"}; //참가자
int startS,finishS; //시작 초,끝나는 초
int record [] = new int[2]; //기록 저장
System.out.println("10초에 가까운 사람이 이기는 게임입니다.");
for(int i=0;i<name.length;i++) {
System.out.print(name[i]+" 시작 <Enter>키>>");
enter = sc.nextLine();
startS = Second();
System.out.println("\t현재 초 시간 = "+startS);
System.out.print("10초 예상 후 <Enter>키>>");
enter = sc.nextLine();
finishS = Second();
System.out.println("\t현재 초 시간 = "+finishS);
if(startS>finishS) {
record[i] = Math.abs(finishS+60-startS);
}
else {
record[i] = Math.abs(startS-finishS);
}
}
int win = 0;
for(int i=0;i<name.length;i++) {
System.out.print(name[i]+"의 결과 "+record[i]+",");
if(Math.abs(record[win]-10)>Math.abs(record[i]-10)) {
win = i;
}
}
System.out.println("승자는 " +name[win]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ex6 e = new ex6();
e.run();
}
}
결과
10초에 가까운 사람이 이기는 게임입니다.
황기태 시작 <Enter>키>>
현재 초 시간 = 50
10초 예상 후 <Enter>키>>
현재 초 시간 = 59
이기문 시작 <Enter>키>>
현재 초 시간 = 1
10초 예상 후 <Enter>키>>
현재 초 시간 = 15
황기태의 결과 9,이기문의 결과 14,승자는 황기태
'명품JAVA프로그래밍 > 6장 모듈과 패키지 개념' 카테고리의 다른 글
[명품JAVA프로그래밍] 6장 실습문제 8번 (0) | 2022.01.24 |
---|---|
[명품JAVA프로그래밍] 6장 실습문제 7번 (0) | 2022.01.23 |
[명품JAVA프로그래밍] 6장 실습문제 5번 (0) | 2022.01.23 |
[명품JAVA프로그래밍] 6장 실습문제 4번 (0) | 2022.01.23 |
[명품JAVA프로그래밍] 6장 실습문제 3번 (0) | 2022.01.23 |