前提・実現したいこと
java初心者です。
プレイヤーとCPUでじゃんけんをし、
結果を判定できるようにしたいです。
発生している問題・エラーメッセージ
int res = Integer.parseInt(str); がうまく機能しません。resがなぜか変数として宣言できていないようです。 Janken1.java:35: エラー: シンボルを見つけられません case 1:if(res == gu){ ^ シンボル: 変数 res 場所: クラス Janken1 Janken1.java:37: エラー: シンボルを見つけられません else if(res == choki){
該当のソースコード
java
1import java.io.*; 2 3class Janken1 4{ 5 public static void main(String[] args) 6 { 7 8 final int gu = 1; 9 final int choki = 2; 10 final int pa = 3; 11 System.out.println("じゃんけんしよう!"); 12 System.out.println("「gu」「choki」「pa」を打ち込んでください"); 13 14 try{ 15 BufferedReader br = 16 new BufferedReader 17 (new InputStreamReader(System.in)); 18 19 String str = br.readLine(); 20 int res = Integer.parseInt(str); 21 System.out.println("あなたが出したのは「" +str+ "」です。"); 22 } 23 catch(IOException e){ 24 System.out.println("入力エラーです。" ); 25 } 26 int num = (int) (Math.random()*3)+1; 27 if(num == 1 ){ 28 System.out.println("私が出したのは「gu」です!!"); 29 }else if(num == 2){ 30 System.out.println("私が出したのは「choki」です!!"); 31 }else if(num == 3){ 32 System.out.println("私が出したのは「pa」です!!"); 33 } 34 switch(num){ 35 case 1:if(res == gu){ 36 System.out.println("あいこです。");} 37 else if(res == choki){ 38 System.out.println("あなたの負けです。");} 39 else if(res == pa){ 40 System.out.println("あなたの勝ちです。");} 41 break; 42 case 2:if(res == gu){ 43 System.out.println("あなたの勝ちです。");} 44 else if(res == choki){ 45 System.out.println("あいこです。");} 46 else if(res == pa){ 47 System.out.println("あなたの負けです。");} 48 break; 49 case 3:if(res == gu){ 50 System.out.println("あなたの負けです。");} 51 else if(res == choki){ 52 System.out.println("あなたの勝ちです。");} 53 else if(res == pa){ 54 System.out.println("あいこです。");} 55 break; 56 } 57 } 58} 59
試したこと
resを他の単語に変えましたが、意味がなかったです。
補足情報(FW/ツールのバージョンなど)
やさしいjavaを参考にしています。他にもおかしなところがあればご指摘いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/06 13:09
2018/03/06 13:14
2018/03/06 13:17
退会済みユーザー
2018/03/06 13:46
2018/03/06 13:51
2018/03/12 16:00