난수 발생하여 컴퓨터와 가위바위보 게임하기
package p0415;
import java.util.Scanner;
import java.util.Random;
public class RandomRPS {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int rand = 0;
String com = null;
while(true) {
System.out.print("사용자: ");
String user = scan.next();
rand = (int)(Math.random()*3)+1;
if(rand==1) {
com = "가위";
System.out.println("컴퓨터: " + com);
}
if(rand==2) {
com = "바위";
System.out.println("컴퓨터: " + com);
}
if(rand==3) {
com = "보";
System.out.println("컴퓨터: " + com);
}
if(com.equals("가위")&&user.equals("보") || com.equals("바위")&&user.equals("가위") || com.equals("보")&&user.equals("바위")) {
System.out.println("컴퓨터가 이겼습니다.");
}
else if(com.equals(user)) {
System.out.println("비겼습니다.");
}
else {
System.out.println("사용자가 이겼습니다.");
}
System.out.print("게임을 계속할까요? (Y/N) ");
String yn = scan.next();
if(yn.equals("N") || yn.equals("n")) {
break;
}
} //end while
System.out.print("게임을 종료합니다.");
}
}
'IT > Java' 카테고리의 다른 글
Java (for문_소수체크하는프로그램) (0) | 2021.04.16 |
---|---|
Java (for문_팩토리얼) (0) | 2021.04.16 |
Java (If else문_가위바위보게임) (0) | 2021.04.15 |
Java (while문&Switch case문_예금, 출금, 잔고 확인) (0) | 2021.04.09 |
Java (while문_증속, 감속, 종료) (0) | 2021.04.09 |