前提
暗算の問題をランダムに出題するプログラムを作っています。
エラー
ローカル変数kotaeが初期化されていない可能性があります。
というエラーが出ます。
同じブロック内(public static void main(String[] args) {})にあると思うのですが。なぜこのようなエラーが出るのでしょうか。また、解決方法を教えていただきたいです。
(可能でしたらこの箇所以外でもなにかありましたらお願いします。)
該当のソースコード
import java.util.Random; import java.util.Scanner; public class Hello{ public static boolean retry() { try(Scanner stdIn = new Scanner(System.in)){ int retry; do { System.out.println("もう一度?Yes:1/No:0"); retry = stdIn.nextInt(); }while(retry != 1 && retry != 0); return retry == 1; } } public static void main(String[] args) { try(Scanner stdIn = new Scanner(System.in)){ Random rand = new Random(); System.out.println("暗算テスト"); int kotae; do { int x = rand.nextInt(100) + 1; int y = rand.nextInt(100) + 1; int z = rand.nextInt(100) + 1; int pattern = rand.nextInt(4); switch(pattern) { case 0 : kotae = x + y + z; break; case 1 : kotae = x + y - z; break; case 2 : kotae = x - y + z; break; case 3 : kotae = x - y - z; break; } while(true) { System.out.print(x + ((pattern < 2) ? "+" : "-") + y + ((pattern % 2 == 0) ? "+" : "-") + z + "="); int answer = stdIn.nextInt(); if(answer == kotae) break; //ここにエラーが出ます。 System.out.println("違いますよ"); } }while (retry()); } } }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/20 11:51
2018/03/20 11:57
2018/03/20 12:45