回答編集履歴

3

追記

2022/09/03 07:33

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -82,3 +82,7 @@
82
82
  }
83
83
  ```
84
84
 
85
+ getRandomHandの引数をStrategyにしてやると癖あるコンピューターが作れます。
86
+ また、日本の3すくみのジャンケン以外も比較的簡単に作れます。
87
+
88
+ いろいろ改造してみると楽しいですよ。

2

修正

2022/09/03 07:29

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -36,11 +36,11 @@
36
36
  if(hand.name.equals(name)) return hand;
37
37
  }
38
38
  throw new IllegalArgumentException(
39
- String.format("不正な入力: %s", name));
39
+ String.format("不正な引数: %s", name));
40
40
  }
41
41
 
42
42
  public static JankenHand getRandomHand(Random gen) {
43
- return new JankenHand[] {P, G, T}[gen.nextInt(3)];
43
+ return JankenHand.values()[gen.nextInt(JankenHand.values().length)];
44
44
  }
45
45
  }
46
46
 
@@ -70,7 +70,7 @@
70
70
  System.out.println("あなたの勝ち");
71
71
  break;
72
72
  }
73
- else if(compHand.win(myHand)) {
73
+ if(compHand.win(myHand)) {
74
74
  System.out.println("コンピュータの勝ち");
75
75
  continue;
76
76
  }

1

追記

2022/09/03 07:24

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -1,2 +1,84 @@
1
1
  文字列の比較には == ではなく .equals を使ってください。
2
2
  具体的には、例えば `myHand == "G"` を `myHand.equals("G")` に書き換えて下さい。
3
+
4
+ おまけ
5
+ ---
6
+ たぶん私ならこう書きます。
7
+ 勝ち負けの判定とか、文字や数値からの変換とか、逐一考えるのはめんどくさそうなので...
8
+ ```Java
9
+ import java.io.BufferedReader;
10
+ import java.io.InputStreamReader;
11
+ import java.io.IOException;
12
+ import java.util.Random;
13
+
14
+ enum JankenHand {
15
+ P("P"), G("G"), T("T");
16
+
17
+ private String name;
18
+ private JankenHand(String name) {
19
+ this.name = name;
20
+ }
21
+
22
+ public boolean win(JankenHand other) {
23
+ if(this == P && other == G) return true;
24
+ if(this == G && other == T) return true;
25
+ if(this == T && other == P) return true;
26
+ return false;
27
+ }
28
+
29
+ @Override
30
+ public String toString() {
31
+ return name;
32
+ }
33
+
34
+ public static JankenHand of(String name) {
35
+ for(JankenHand hand: JankenHand.values()) {
36
+ if(hand.name.equals(name)) return hand;
37
+ }
38
+ throw new IllegalArgumentException(
39
+ String.format("不正な入力: %s", name));
40
+ }
41
+
42
+ public static JankenHand getRandomHand(Random gen) {
43
+ return new JankenHand[] {P, G, T}[gen.nextInt(3)];
44
+ }
45
+ }
46
+
47
+ class Sample {
48
+ public static void main(String[] args) throws IOException {
49
+ Random randGen = new Random();
50
+
51
+ try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
52
+ while(true) {
53
+ // 自分の手を入力
54
+ System.out.print("パー(P),グー(G),チョキ(T)>");
55
+ final JankenHand myHand;
56
+ try {
57
+ myHand = JankenHand.of(br.readLine());
58
+ }
59
+ catch(IllegalArgumentException e) {
60
+ System.err.println(e.getMessage());
61
+ continue;
62
+ }
63
+
64
+ // コンピュータの手を取得
65
+ final JankenHand compHand = JankenHand.getRandomHand(randGen);
66
+ System.out.println("コンピュータ:" + compHand);
67
+
68
+ // 勝ち負けを判定
69
+ if(myHand.win(compHand)) {
70
+ System.out.println("あなたの勝ち");
71
+ break;
72
+ }
73
+ else if(compHand.win(myHand)) {
74
+ System.out.println("コンピュータの勝ち");
75
+ continue;
76
+ }
77
+
78
+ System.out.println("あいこ");
79
+ }
80
+ }
81
+ }
82
+ }
83
+ ```
84
+