import java.util.Random; import java.util.Scanner; public class Ex9_2 { public static void main(String[] args) { Scanner si = new Scanner(System.in); char usr, cmp; // じゃんけん:usr=ユーザーの手,cmp=PCの手:'g','c','p' char flg = 'y'; // 繰り返しフラグ System.out.println("=================="); System.out.println(" じゃんけんゲーム "); System.out.println("=================="); System.out.println("\tねーねー,じゃんけんしようよ!"); while( flg == 'y' ) { // flg = 'y' である限り繰り返し System.out.print("\t [グー = g, チョキ = p, パー = p ] 何を出す? => "); usr = si.nextLine().charAt(0); // ユーザーの入力手を取得し代入 cmp = comp_janken(); // PCの手を計算メソッド呼び出し,戻り値をcmpに代入 hantei(usr, cmp); // 勝ち負け判定メソッド呼び出し System.out.print("\tもう一度やりますか? [ y / n ] => "); flg = si.nextLine().charAt(0); } } // コンピュータの手を計算する:引数 = なし,戻り値 = PCの手[char型] public static char comp_janken() { Random rnd = new Random(); char COMP = 'g'; switch (rnd.nextInt(3)) { case 0: COMP = 'g'; break; case 1: COMP = 'c'; break; case 2: COMP = 'p'; break; default: break; } return COMP; // PCの手を char型で返却 } // 勝ち負け判定メソッド public static void hantei(char USER, char COMP) { System.out.print("\n\t You [" + USER + "] v.s. [" + COMP + "] PC \t 結果は… "); if ( USER == COMP ) { System.out.println("あいこ"); } else if ( (USER=='g' && COMP=='c') || (USER=='c' && COMP=='p') || (USER=='p' && COMP=='g') ) { System.out.println("あんたの勝ち!"); } else { System.out.println("あんたの負け!"); } } }