前回の数当てゲームとは異なる数当てゲームを作ります。ゲームのルールは次の通りです。
フローチャートを参考にプログラムを完成させる。
import java.io.*; import java.text.*; class HitAndBlow { public static void main(String args[]) throws IOException, ParseException{ int ningenNoTe; // 人間の手 int pc[] = {-1, -1, -1, -1};// PCの手を格納 int nin[] = {-1, -1, -1, -1};// 人間の手を格納 int cnt; // 入力した回数用 int atari = 0; // ヒット数カウント int i, j, B, tmp; // ループ用(iとj)、ブロー用, 一時記憶用 BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); DecimalFormat DF = new DecimalFormat(); String s; System.out.println("★★★★★★★★★★"); System.out.println("ヒット&ブロー"); // ---------------------------------------- <1> while( <2> ){ j = 0; tmp = (int)(9 * Math.random()); while( <3> ){ if( <4> ){ tmp = (int)(9 * Math.random()); <5> }else{ <6> } } <7> <8> } // ---------------------------------------- <9> <10>{ atari = 0; System.out.print("4桁の数を入力: "); s = BR.readLine(); Number N = DF.parse(s); ningenNoTe = N.intValue(); // ---------------------------------------- i = 0; j = 1000; while( <11> ){ nin[i] = ningenNoTe / j; <12> j /= 10; i = i + 1; } // ---------------------------------------- i = 0; while( <13> ){ <14>{ atari = atari + 1; <15> } i = i + 1; } B = 0; i = 0; <16>{ j = 0; <17>{ <18>{ B = B + 1; } <19> } <20> } // ---------------------------------------- System.out.print(" Hit = "); System.out.print(atari); System.out.print(" : Blow "); System.out.print(B); System.out.print(" : "); cnt = cnt + 1; } // ---------------------------------------- System.out.println("\n★★ 終了! ★★"); System.out.println(cnt + "回目で正解!"); } }