개발일지/Java + Spring
switch문 - 가위바위보
연습용365
2021. 10. 18. 11:26
//가위1 바위2 보3 컴퓨터는 랜덤 //빼기 이용 하기
System.out.print("가위(1), 바위(2), 보(3) 중 하나를 입력하세요.> ");
Scanner sc = new Scanner(System.in);
int user = sc.nextInt();
int com = (int)(Math.random() * 3)+1;
//매쓰는 실수라서 강제 형변환하고, 원하는 숫자 넣은 후 더하기 1을 해야 됨(0부터 시작)
System.out.printf("당신은 %d입니다.\n", user);
System.out.printf("컴은 %d입니다.\n", com);
switch(user-com) {
case -1:
case 2:
System.out.println("컴승");
break;
case -2:
case 1:
System.out.println("유저승");
break;
default : System.out.println("무승부 입니다.");
Math.random()
클래스이름 . 메소드이름()
if(user != com)
{
if((user==1&&com==3)||(user==2&&com==1)||(user==3&&com==2))
{
System.out.println("유저 승");
}
else if((user==1&&com==2)||(user==2&&com==3)||(user==3&&com==1))
{
System.out.println("컴승");
}
}
else
{
System.out.println("무승부");
}