前提・実現したいこと
乱数と入力した4桁の数字の配列をfor文で回して同じ桁に同じ数字がある場合〇違う桁に同じ数字がある場合△となるプログラムを作りたいのですがforまでいかず
数字を入力して無限ループになります
該当のソースコード
java
1package game; 2 3import java.util.ArrayList; 4import java.util.Arrays; 5import java.util.List; 6import java.util.Scanner; 7 8public class MathGame { 9 10 public static void main(String[] args) { 11 // TODO 自動生成されたメソッド・スタブ 12 System.out.println("ゲームを始めます"); 13 int answer[] = new int[4]; 14 answer[0] = (int) (Math.random() * 9) + 1; 15 for (int i = 1; i < answer.length; i++) { 16 int num = -1; 17 for (boolean isNotExist = false; !isNotExist;) { 18 num = (int) (Math.random() * 10); 19 isNotExist = true; 20 for (int j = 0; j < answer.length; j++) { 21 if (answer[j] == num) { 22 isNotExist = false; 23 break; 24 } 25 } 26 } 27 if (num < 0) { 28 // ここにきたらバグ 29 throw new IllegalStateException(); 30 } 31 answer[i] = num; 32 } 33 Arrays.stream(answer).forEach(System.out::println); 34 35 int input[] = new int[4]; 36 boolean validated = false; 37 while (!validated) { 38 int a = 0; 39 for (int n = 0; a < input.length; a++) { 40 System.out.println("4文字の数字を入力してください"); 41 String sc = new Scanner(System.in).nextLine(); 42 if (sc.length() == 4) { 43 System.out.println("4文字だけ入力されました。"); 44 validated = true; 45 } else { 46 System.out.println("4文字だけ入力してください。"); 47 break; 48 } 49 } 50 } 51 List<Integer> result = new ArrayList<Integer>(); 52 for(int a : answer){ 53 for(int b : input){ 54 if(a == b){ 55 result.add(a); 56 break; 57 } 58 } 59 } 60 System.out.println("ListA = " + answer); 61 System.out.println("ListB = " + input); 62 System.out.println("ListC = " + result); 63 } 64 } 65