質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

4603閲覧

javaでじゃんけんGame3回勝負あっちむいてホイを作っているのですがあっちむいてほいの負けたときのあなたの負けが表示されない

ta.tatu

総合スコア12

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2020/03/10 11:55

編集2020/03/10 12:10

import

1import java.util.Random; 2import java.util.Scanner; 3 4public class test32 { 5 6 public static void main(String[] args) { 7 int win = 0; 8 int game = 3; 9 10 for(int i = 0; i < game; i++) { 11 win += sub (); 12 } 13 14 System.out.println(game + "回勝負中、あなたは" + win + "回勝ちました。"); 15 } 16 17 static Scanner stdIn = new Scanner(System.in); 18 static Random rand = new Random(); 19 20 private static int sub() { 21 String[] com1 = new String[] {"グー", "チョキ", "パー"}; 22 int hantei = 0; 23 24 while(true) { 25 26 while(true) { 27 System.out.println("じゃんけん勝負\n3回勝負です\n\n初めにじゃんけん!です\n数字で入力してね"); 28 System.out.println("0:グー\n1:チョキ\n2:パー\n"); 29 30 System.out.print("最初はグー、じゃんけん : "); 31 32 try { 33 String str1 = stdIn.next(); 34 int pry1 = Integer.parseInt(str1); 35 int cpu1 = rand.nextInt(3); 36 37 switch (pry1) { 38 case 0: 39 if (cpu1 == 0) { 40 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "グーで… " + "あいこだよ!\n"); 41 hantei = 0; 42 } else if (cpu1 == 1) { 43 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "グーで… " + "あなたの勝ち\n"); 44 hantei = 1; 45 } else { 46 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "グーで… " + "あなたの負け\n"); 47 hantei = 2; 48 } 49 break; 50 case 1: 51 if (cpu1 == 0) { 52 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "チョキで… " + "あなたの負け\n"); 53 hantei = 2; 54 } else if (cpu1 == 1) { 55 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "チョキで… " + "あいこだよ!\n"); 56 hantei = 0; 57 } else { 58 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "チョキで… " + "あなたの勝ち\n"); 59 hantei = 1; 60 } 61 break; 62 case 2: 63 if (cpu1 == 0) { 64 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "パーで… " + "あなたの勝ち\n"); 65 hantei = 1; 66 } else if (cpu1 == 1) { 67 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "パーで… " + "あなたの負け\n"); 68 hantei = 2; 69 } else { 70 System.out.println(com1[cpu1] + "(COM)と" + "(Player)" + "パーで… " + "あいこだよ!\n"); 71 hantei = 0; 72 } 73 break; 74 default: 75 System.out.println("範囲外の数字です。0~2で入力してね。"); 76 break; 77 } 78 79 if(hantei != 0) { 80 break; 81 } 82 }catch(NumberFormatException e) { 83 System.out.println("数字で入力してね。"); 84 } 85 } 86 87 /*|----- ここまでが、じゃんけん!の領域 -----|*/ 88 89 /*|----- ここから、あっちむいてホイ!の領域 -----|*/ 90 String[] com2 = new String[] {"上", "下", "左", "右"}; 91 92 while(true) { 93 System.out.println("次はあっちむいてホイ!です\n数字で入力してね"); 94 System.out.println("0:上\n1:下\n2:左\n3:右\n"); 95 96 System.out.print("始めるよ! あっちむいてホイ : "); 97 try { 98 String str2 = stdIn.next(); 99 int pry2 = Integer.parseInt(str2); 100 int cpu2 = rand.nextInt(4); 101 102 switch (pry2) { 103 case 0: 104 if (cpu2 == 0) { 105 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "上だね\n"); 106 if(hantei == 1) { 107 System.out.println("あなたの勝ち!"); 108 return 1; 109 }else if(hantei == 2) { 110 System.out.println("あなたの負け!"); 111 return 0; 112 } 113 } else if (cpu2 == 1) { 114 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "上だね\nもう一度じゃんけんからだよ!"); 115 } else if (cpu2 == 2) { 116 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "上だね\nもう一度じゃんけんからだよ!"); 117 } else if (cpu2 == 3) { 118 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "上だね\nもう一度じゃんけんからだよ!"); 119 } 120 break; 121 case 1: 122 if (cpu2 == 0) { 123 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "下だね\nもう一度じゃんけんからだよ!"); 124 } else if (cpu2 == 1) { 125 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "下だね\n"); 126 if(hantei == 1) { 127 System.out.println("あなたの勝ち!"); 128 return 1; 129 }else if(hantei == 2) { 130 System.out.println("あなたの負け!"); 131 return 0; 132 } 133 } else if (cpu2 == 2) { 134 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "下だね\nもう一度じゃんけんからだよ!"); 135 } else if (cpu2 == 3) { 136 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "下だね\nもう一度じゃんけんからだよ!"); 137 } 138 break; 139 case 2: 140 if (cpu2 == 0) { 141 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "左だね\nもう一度じゃんけんからだよ!"); 142 } else if (cpu2 == 1) { 143 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "左だね\nもう一度じゃんけんからだよ!"); 144 } else if (cpu2 == 2) { 145 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "左だね\n"); 146 if(hantei == 1) { 147 System.out.println("あなたの勝ち!"); 148 return 1; 149 }else if(hantei == 2) { 150 System.out.println("あなたの負け!"); 151 return 0; 152 } 153 } else if (cpu2 == 3) { 154 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "左だね\nもう一度じゃんけんからだよ!"); 155 } 156 break; 157 case 3: 158 if (cpu2 == 0) { 159 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "右だね\nもう一度じゃんけんからだよ!"); 160 } else if (cpu2 == 1) { 161 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "右だね\nもう一度じゃんけんからだよ!"); 162 } else if (cpu2 == 2) { 163 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "右だね\nもう一度じゃんけんからだよ!"); 164 } else if (cpu2 == 3) { 165 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + "右だね\n"); 166 if(hantei == 1) { 167 System.out.println("あなたの勝ち!"); 168 return 1; 169 }else if(hantei == 2) { 170 System.out.println("あなたの負け!"); 171 return 0; 172 } 173 } 174 break; 175 default: 176 System.out.println("範囲外の数字です。0~3で入力してね。"); 177 break; 178 } 179 }catch(InputMismatchException e) { 180 System.out.println("エラーです!0~3の数字で入力してね"); 181 }catch(NumberFormatException e) { 182 System.out.println("エラーです!0~3の数字を入力してね"); 183 } 184 return 0; 185 } 186 } 187 } 188} 189コード

あっちむいてほいのあなたの負けを表示したいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yureighost

2020/03/10 12:32

試してみましたけど、ちゃんと表示されますよ。
ta.tatu

2020/03/10 12:35

本当ですか?!Σ( ̄□ ̄;) すいません確認不足かな⁉️
ta.tatu

2020/03/10 12:50

今確認しましたがじゃんけんの時のあなたの負けは表示されるのですが、あっちむいてほいの時のあなたの負けが表示されませんでした。
jimbe

2020/03/10 18:49

あっちむいてほいは, pry と cpu が同じ値の時に, じゃんけんの結果を「勝ち負け」と表示して, pry と cpu が違う値の時には(勝ち負けは無く)「もう一度じゃんけん」と表示していますが, これはどういうことでしょう.
jimbe

2020/03/11 05:51

すいません, あっちむいてほいの流れを忘れていました.
guest

回答2

0

まず, 長すぎです.
ご質問の件は, 条件を並べ過ぎてどの場合にどれが表示されるのかが分からなくなっているためと思います.
例えば switch(pry2) { ~ } の部分は

java

1 if(pry2 < 0 || com2.length < pry2) { 2 System.out.println("範囲外の数字です。0~3で入力してね。"); 3 } else { 4 System.out.println(com2[cpu2] + "(COM)と" + "(Player)" + com2[pry2] + "だね"); 5 if (pry2 == cpu2) { 6 if(hantei == 1) { 7 System.out.println("あなたの勝ち!"); 8 return 1; 9 }else if(hantei == 2) { 10 System.out.println("あなたの負け!"); 11 return 0; 12 } 13 } else { 14 System.out.println("\nもう一度じゃんけんからだよ!"); 15 break; //これが必要では? 16 } 17 }

と纏まります.
そして, おそらく「もう一度じゃんけんから」となった場合は(3回勝負のカウントを上げずに再度じゃんけんをする為に) あっちむいてほいの while ループを抜けるため break を書かなくてはいけないと思うのですが, case 内では書けませんので, このように纏めたほうがよいのではと思います.
また, ご提示のコードの最後にある return 0 は削除しないと, 数字以外や範囲外の数値を入力等の場合に3回勝負のカウントが上がってしまいます.

モロモロ想像・修正すると, 以下の様になりました.
(遊びで日本語メソッド名を使いました. 問題ありましたらテキトウな英文字にしてください.)

java

1import java.util.*; 2 3class Player { 4 private Scanner scanner = new Scanner(System.in); 5 6 int select(String[] choices, String prompt) { 7 while(true) { 8 for(int i=0; i<choices.length; i++) System.out.printf("%d:%s\n", i, choices[i]); 9 System.out.print(prompt); 10 try { 11 String str = scanner.next(); 12 int pry = Integer.parseInt(str); 13 if(0 <= pry && pry < choices.length) return pry; 14 15 System.out.println("範囲外の数字です。0~" + choices.length + "で入力してね。"); 16 }catch(NumberFormatException e) { 17 System.out.println("エラーです!0~" + choices.length + "の数字を入力してね"); 18 } 19 } 20 } 21} 22 23class Computer { 24 private Random rand = new Random(); 25 26 int select(String[] choices) { 27 return rand.nextInt(choices.length); 28 } 29} 30 31enum Settlement { 32 NOT_SETTLED, WON, LOST 33} 34 35public class test32 { 36 public static void main(String[] args) { 37 Player player = new Player(); 38 Computer computer = new Computer(); 39 int win = 0; 40 int game = 3; 41 42 System.out.println("じゃんけん勝負\n" + game + "回勝負です"); 43 44 for(int i=1; i<=game; i++) { 45 System.out.println("\n---- " + i + "回戦 ----"); 46 Settlement settlement; 47 do { 48 settlement = あっちむいてホイ(player, computer, じゃんけん(player, computer)); 49 } while(settlement == Settlement.NOT_SETTLED); 50 if(settlement == Settlement.WON) win++; 51 } 52 53 System.out.println("\n---- 終了 ----"); 54 System.out.println(game + "回勝負中、あなたは" + win + "回勝ちました。"); 55 } 56 57 private static boolean じゃんけん(Player player, Computer computer) { 58 String[] handStr = new String[] {"グー", "チョキ", "パー"}; 59 int[] hantei = new int[] { 1, 2, 0 }; //ドレに勝つか 60 61 System.out.println("\n初めにじゃんけん!です\n数字で入力してね"); 62 63 while(true) { 64 int pry = player.select(handStr, "最初はグー、じゃんけん : "); 65 int cpu = computer.select(handStr); 66 System.out.print(handStr[cpu] + "(COM)と" + "(Player)" + handStr[pry] + "で… "); 67 68 if (pry != cpu) return printResult(hantei[pry] == cpu); 69 70 System.out.println("あいこだよ!\n"); 71 } 72 } 73 74 private static Settlement あっちむいてホイ(Player player, Computer computer, boolean playerAttack) { 75 String[] dirStr = new String[] {"上", "下", "左", "右"}; 76 77 System.out.println("\n次はあっちむいてホイ!です\nあなたは"+(playerAttack?"攻撃側":"防御側")+"です\n数字で入力してね"); 78 79 int pry = player.select(dirStr, "始めるよ! あっちむいてホイ : "); 80 int cpu = computer.select(dirStr); 81 System.out.println(dirStr[cpu] + "(COM)と" + "(Player)" + dirStr[pry] +"だね"); 82 83 if(pry != cpu) { 84 System.out.println("もう一度じゃんけんからだよ!"); 85 return Settlement.NOT_SETTLED; 86 } 87 return printResult(playerAttack) ? Settlement.WON : Settlement.LOST; 88 } 89 90 private static boolean printResult(boolean result) { 91 System.out.println("あなたの" + (result?"勝ち":"負け") + "!"); 92 return result; 93 } 94}

投稿2020/03/10 19:25

編集2020/03/11 07:01
jimbe

総合スコア12696

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

コンソールのスクショで申し訳ありませんが、
この動作の想定ではないんですかね。
イメージ説明

投稿2020/03/10 13:02

yureighost

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ta.tatu

2020/03/10 13:16

有り難うございます。出てますね! 何故だろう? ベストアンサーにさせて頂きます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問