import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Solver { // 回答 private int[] answer; // Blow の数 private int blow; // Hit の数 private int hit; // 正解かどうかを示すフラグ private boolean result; public Solver(){ answer = new int[4]; } // 回答する // 回答はユーザに入力してもらう public int[] getAnswer(){ System.out.println("数字を入力してください"); System.out.print(">"); // リーダの設定 // 標準入力からリーダを取り出して使用する InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(inputStreamReader); while(true){ try{ String answerText = reader.readLine(); boolean checkFlag = true; for(int i = 0 ; i < answerText.length() ; i++){ int number; try{ number = Integer.parseInt(answerText.substring(i, i+1)); }catch(NumberFormatException ex){ checkFlag = false; System.out.println("数字以外の文字が使われています"); System.out.println("もう一度入力してください"); System.out.print(">"); break; } // 同じ数字が使われていないかのチェック for(int j = 0 ; j < i ; j++){ if(number == answer[j]){ checkFlag = false; System.out.println("同じ数字が複数回使われています"); System.out.println("もう一度入力してください"); System.out.print(">"); break; } } if(checkFlag){ answer[i] = number; }else{ break; } } if(checkFlag){ return answer; } }catch(IOException ex){ ex.printStackTrace(); System.exit(1); } } } // Blow, Hit を設定 public void setBlowAndHit(int blow, int hit){ this.blow = blow; this.hit = hit; System.out.println("Blow : " + blow + " Hit : " + hit); } // 結果を設定する public void setResult(boolean result){ this.result = result; if(result){ System.out.println("正解です!!!!"); } } }