콘솔로 가위바위보를 할 수 있는 프로그램입니다.
랜덤으로 1~3까지의 수를 뽑아주고
이것을 배열에 담고 0~2 번째 순서로 가위 바위 보를 집어넣어줬습니다
이후 switch case 문을 사용해서 내가 입력한값과 컴퓨터가 출력한 값을 비교해 주었습니다.
import java.util.Scanner;
public class gwaiBaiBo {
enum Gwai{가위,바위,보}
public gwaiBaiBo(){
int e=0;
String com = null;
for (Gwai f:Gwai.values())
System.out.print(f );
System.out.println("중에 하나를 입력하세요. 게임을 종료하시려면 0을 눌러주세요");
int[] a=new int[3];
a[0]=1;//가위
a[1]=2;//바위
a[2]=3;//보
while(true){
e = (int) (Math.random()*3);
if(a[e]==1){
com="가위";
}
else if(a[e]==2){
com="바위";
}
else if (a[e]==3){
com="보";
}
Scanner nm=new Scanner(System.in);
String r = nm.next();
switch (r){
case "가위":
if (com=="가위"){
System.out.println("비겼습니다");
new gwaiBaiBo();
}
else if (com=="바위"){
System.out.println("졌습니다.");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
else if (com=="보"){
System.out.println("이겼습니다");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
break;
case "바위":
if (com=="바위")
{
System.out.println("비겼습니다");
new gwaiBaiBo();
}
else if (com=="보"){
System.out.println("졌습니다.");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
else if(com=="가위") {
System.out.println("이겼습니다");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
break;
case "보":
if (com=="보"){
System.out.println("비겼습니다");
new gwaiBaiBo();
}
else if (com=="가위"){
System.out.println("졌습니다.");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
else if(com=="바위") {
System.out.println("이겼습니다");
System.out.println(" 컴퓨터는 "+com+"였습니다");
new gwaiBaiBo();
}
break;
case "0":
System.exit(0);
break;
default :
System.out.println("잘못 했습니다");
new gwaiBaiBo();
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new gwaiBaiBo();
}
}
댓글