2020.10.28 고급자바 과제 Thread 가위바위보 카운트다운

선생님 코드 : 

//배열로 인덱스를 활용하기 

//난수를 이용해서 컴퓨터의 가위 바위 보를 정한다. 

    String[] data = {"가위" , "바위" , "보 } ; 

    int index = (int)(Math.random() * 3 ) ; 

    String com = data[index] ; 


//사용자로부터 가위 바위 보 입력받기 

String man; 

do { 

    man = Joptionpane.showInputDialog ( " 가위  바위 보를 입력하세요. " ) ; 

} while (      !(man.equals("가위") || man.equals("바위") || man.equals("보")  )   ) ; 


// 위에 while 문과 동일 : while (!man.equals("가위") && !man.equals("바위") && !man.equals("보")    ) ;

-> 가위와 바위와 보를 입력하지 않았고 아무거나 입력했을때는 계속 입력이 안되고 반복이된다. 


//결과를 판정하기 


String result = "" ; //결과가 저장될 변수 선언 

if(man.equals(com) ) { 

    result = "비겼습니다." ; 

}else if ( man.equals("가위") && com.equals("보") ||

            man.equals("바위") && com.equals("가위") ||

            man.equals("보") && com.equals("보" ) ) {

        result = "이겼습니다. " ; 

 } else { 

    result = "당신이 졌습니다." ; 














----------------------------------------------------------------------------------------------------

 package kr.or.ddit.basic;


import javax.swing.JOptionPane;


public class ThreadTest07 {


/*

*문제 ) 컴퓨터와 가위 바위 보를 진행하는 프로그램을 작성하시오.  

* -컴퓨터의 가위 바위 보는 난수를 이용해서 구하고 

* -사용자는 showInputDialog() 메서드를 이용해서 입력을 받는다. 

*  -입력시간은 5c 로 제한하고 카운트 다운을 진행한다. 

*  -5초 안에 입력이 없으면 게임에 진것으로 처리하고 끝낸다. 

*  -5초 안에 입력을 하면 승패를 구해서 출력한다.  

*  

*/

public static void main(String[] args) {

Thread th1 = new DataInput2();

Thread th2 = new CountDown2(); 

th1.start();

th2.start();

}


}


//데이터를 입력하는 쓰레드 


class DataInput2 extends Thread {

public static boolean inputCheck; 

@Override

public void run() {

String str = JOptionPane.showInputDialog(" 10초 내에 가위 바위 보 중에 입력하세요"); 

int computer = (int)(Math.random() * 2 + 1 ); 

String computerPut ; 

if (computer ==1) {

computerPut = "가위";

}

else if (computer ==2 ) { 

computerPut = "바위";

}

else {

computerPut = "보"; 

}

inputCheck = true; 

if (str.equals(computerPut)) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("비겼습니다.");

}

//이부분 하나로 만들 수 있음... 생각해봐 

else if (str.equals("가위") ) {

if (computerPut.equals("바위") ) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 졌습니다..");

}

else if (computerPut.equals("보") ) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 이겼습니다.");

}

}

else if (str.equals("바위") ) {

if (computerPut.equals("가위")) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 이겼습니다.");

}

else if (computerPut.equals("보")) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 졌습니다.");

}

}

else if (str.equals("보") ) {

if (computerPut.equals("가위") ) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 졌습니다.");

}

else if (computerPut.equals("바위") ) {

System.out.println("사용자 : " + str + "컴퓨터 : " + computerPut );

System.out.println("당신이 이겼습니다..");

}

}

}

}





//카운트 다운을 진행하는 쓰레드 


class CountDown2 extends Thread {


@Override

public void run() {

for ( int i = 10 ; i >=1; i-- ) {

if(DataInput2.inputCheck == true) {

return; 

}

System.out.println(i);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

}

}

System.out.println("10초가 지났습니다.");

System.exit(0);

}

}


댓글