반응형

version1

package chap03;
import java.util.Scanner;
import java.util.Random;
public class plus {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		Random rand = new Random();
		char user='a'; 
		String d = "무승부 ";
		String b = "승리";
		String c = "패배";
		while((user != 'Q')&&(user !='q')) {
			System.out.println("가위는 1, 바위는 2, 보는 3");
			System.out.println("종료를 원하시면 q를 눌러주세요. ");
			System.out.print("숫자를 입력하세요: ");
			user = (s.next()).charAt(0);
			int computer= rand.nextInt(3)+1;
			switch(user){
				case '1' : 
						if(computer==2) {
							System.out.println("컴퓨터는 : "+ computer+ " " + c);
							break;
						}
						else if(computer==3) {
							System.out.println("컴퓨터는 : "+ computer+" " + b);
							break;
						}
						else {
							System.out.println("컴퓨터는 "+ computer+ " " + d);
							break;
						}
				case '2' :
						if(computer==1) {
							System.out.println("컴퓨터는 : "+ computer+" " + b);
							break;
						}
						else if(computer==3) {
							System.out.println("컴퓨터는 : "+ computer+" " + c);
							break;
						}
						else {
							System.out.println("컴퓨터는 "+ computer+ " " + d);
							break;
						}
				case '3' : 
						if(computer==1) {
							System.out.println("컴퓨터는 : "+ computer+ " " + c);
							break;
						}
						else if(computer==2) {
							System.out.println("컴퓨터는 : "+ computer+ " " + b);
							break;
						}
						else {
							System.out.println("컴퓨터는 "+ computer + " " + d);
							break;
						}
				}
		}
	}
}

version2

package test01;
import java.util.Scanner;
public class RpsGame {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for(int i=0; i < 3; i++) {
			int com = (int)(Math.random() * 10) % 3 + 1;
			int input;
			while(true) {
				System.out.println("입력 [1: 가위 , 2: 바위 3: 보]");
				input = sc.nextInt();
				if(input >= 1 && input <= 3) break;
				System.out.println("가위, 바위, 보 중 하나만 선택해주세요.");
			}
			System.out.println();
			
			if(com == 1) System.out.println("컴퓨터 : 가위");
			else if(com == 2) System.out.println("컴퓨터 : 바위");
			else System.out.println("컴퓨터 : 보");
			
			if(input == 1) System.out.println("사람 : 가위");
			else if(input == 2) System.out.println("사람 : 바위");
			else System.out.println("사람 : 보");
			
			System.out.println("<<결과>>");
			if(com == input) {
				System.out.println("비김");
			} else if (com==1 && input == 2 || com==2 && input==3 || com==3 && input ==1) { 
				System.out.println("사람 win");
			} else {
				System.out.println("컴퓨터 win");
			}
			System.out.println("=============================");
		}
		sc.close();
	}
}
반응형

+ Recent posts