JAVA 프로그래밍

문제

1초마다 비트가 아래로 떨어지면, A(왼쪽),S(중앙),D(오른쪽) 중 하나를 입력받아 맞추는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요 

프로그램의 실행순서 및 실행상태

1  public static void main( String[] args ) {  

2   thread.start();

B1b  public void run() {






A S D

B11    for( index = beat.length-1; index > 3; index-- ) {
                   main()
   beat  (obj01) 
 thread  (Thread) 
  ()
 this   index  ...  (obj01)

     OOOOO     




A S D

B12     Thread.sleep( 1000 );

E001 // Drop the Beat 스레드 1초간 대기 

E002 // (Drop the Beat 스레드와 별개로) 메인 스레드 2 이후 실행중 

3   beat.hit();

B2b  public void hit() {

B21   do {

B23         scan.nextLine();
     OOOOO     




A S D
A,S,D 중 하나를 입력하고 엔터치세요:
                   main()
   beat  (obj01) 
 thread  (Thread) 
  ()
 this
   in  '' 
  ()
 this   index ... (obj01)

E003 // 메인 스레드 입력 대기
                                              
... 

E004 // 1초 후 Drop the Beat 스레드 활성화

B13    }

B11    for( index = beat.length-1; index > 3; index-- ) {
                   main()
   beat  (obj01) 
 thread  (Thread) 
  ()
 this
   in  '' 
  ()
 this   index ... (obj01)

          OOOOO
OOOOO



A S D
A,S,D 중 하나를 입력하고 엔터치세요:

B12     Thread.sleep( 1000 );

E001 // Drop the Beat 스레드 1초간 대기 
                                              
... 

E005 // 's' 입력 

s

B22    in = 
                   main()
   beat  (obj01) 
 thread  (Thread) 
  ()
 this
   in
  ()
 this   index  10  ... (obj01)

B24   } while( in.equals("a") || in.equals("s") || in.equals("d") || in.equals("A") || in.equals("S") || in.equals("D") );

B21   do {

B23         scan.nextLine();
     OOOOO     
OOOOO
OOOOO
OOOOO
Good!
A S D
A,S,D 중 하나를 입력하고 엔터치세요:

E003 // 메인 스레드 입력 대기
                                              
... 

E007 // 'quit' 입력 

quit

B22    in = 
                   main()
   beat  (obj01) 
 thread  (Thread) 
  ()
 this
   in
  ()
 this   index  8  ... (obj01)

B24   } while( in.equals("a") || in.equals("s") || in.equals("d") || in.equals("A") || in.equals("S") || in.equals("D") );

B2e  }

4  }


프로그램 코드

E001	// Drop the Beat 스레드 1초간 대기 
E002	// (Drop the Beat 스레드와 별개로) 메인 스레드 2 이후 실행중 
E003	// 메인 스레드 입력 대기
E004	// 1초 후 Drop the Beat 스레드 활성화
E005	// 's' 입력 
E006	// 'd' 입력 
E007	// 'quit' 입력 
	import java.util.Scanner;
	
	class Beat implements Runnable {
		private int index;
		private final String[][] beat = { 
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "OOOOO" },
				{ "     ", "OOOOO", "     " },
				{ "     ", "OOOOO", "     " },
				{ "OOOOO", "     ", "     " },
				{ "     ", "     ", "OOOOO" },
				{ "     ", "OOOOO", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
				{ "     ", "     ", "     " },
		};
		
B1b		public void run() {
			try {
				System.out.println( "\033[6;1f  A    S    D " );
B11				for( index = beat.length-1; index > 3; index-- ) {
					System.out.print( "\033[1;1f" );
					System.out.println( beat[index-4][0] + beat[index-4][1] + beat[index-4][2] );
					System.out.println( beat[index-3][0] + beat[index-3][1] + beat[index-3][2] );
					System.out.println( beat[index-2][0] + beat[index-2][1] + beat[index-2][2] );
					System.out.println( beat[index-1][0] + beat[index-1][1] + beat[index-1][2] );
					System.out.println( beat[index  ][0] + beat[index  ][1] + beat[index  ][2] );
					System.out.println( "\033[7;1f\033[7;40f" );
					System.out.flush();
B12					Thread.sleep( 1000 );
B13				}
				System.exit(0);
			} catch( Exception e ) {
			}
B1e		}
		
B2b		public void hit() {
			Scanner scan = new Scanner( System.in );
			String in = "";
B21			do {
				if ( ( in.equals("A") || in.equals("a") ) && beat[index][0].equals("OOOOO") )
					System.out.println( "\033[5;1fGood!");
				else if ( in.equals("A") || in.equals("a") )
					System.out.println( "\033[5;1fMiss!");
				else if ( ( in.equals("S") || in.equals("s") ) && beat[index][1].equals("OOOOO") )
					System.out.println( "\033[5;6fGood!");
				else if ( in.equals("S") || in.equals("s") )
					System.out.println( "\033[5;6fMiss!");
				else if ( ( in.equals("D") || in.equals("d") ) && beat[index][2].equals("OOOOO") )
					System.out.println( "\033[5;11fGood!");
				else if ( in.equals("D") || in.equals("d") )
					System.out.println( "\033[5;11fMiss!");
				System.out.flush();
				System.out.print(  "\033[7;1fA,S,D,Q(uit)중 하나를 입력하고 엔터치세요: " );
B22				in = 
B23				     scan.nextLine();
B24			} while( in.equals("a") || in.equals("s") || in.equals("d") || in.equals("A") || in.equals("S") || in.equals("D") );
			scan.close();
B2e		}
	}
	
	public class RhythmGame
	{
1		public static void main( String[] args ) { 	
			Beat beat = new Beat();
			Thread thread = new Thread( beat );
2			thread.start();
3			beat.hit();
4		}
	}