第一版 笨笨方法,我還沒有理解到【字串】就是【字元】的陣列....

還在把輸入的值轉成陣列,也沒有想到防止重複檢查的方法還有布林陣列

我幾乎把所有功能都複雜化了  

import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;

import java.util.Arrays;
import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ans[] = new int[4];     //題目
        int in = 0;                 //輸入
        int input[] = new int[4];   //將輸入轉為陣列
        int abNum[] = new int[2];   //AB數量
        int count =0;               //計次
        ans = createQuestion();
        for(int i =0; i<ans.length; i++){
            System.out.print(ans[i] +" ");
        }
//        ans = new int[]{7,7,6,7};  //測試行
        while (true) {
            System.out.print("請輸入四個數字:");
            in = sc.nextInt();
            input = createAns(in);
            abNum = checkAB(input, ans);
            if(abNum[0] ==4){
                System.out.println("WIN!!" +"\t猜了" + (count+1) + "");
                break;
            }
            System.out.println(abNum[0] + "A" + abNum[1] + "B");
            count++;
        }
    }
    
    //製作題目
    public static int[] createQuestion() {
        int question[] = new int[4];
        for (int i = 0; i < question.length; i++) {
            question[i] = (int) (Math.random() * 10);
        }
        return question;
    }

    //轉成陣列
    public static int[] createAns(int in) {
        int input[] = new int[4];
        input[3] = in % 10;
        input[2] = (in / 10) % 10;
        input[1] = (in / 100) % 10;
        input[0] = in / 1000;
        return input;
    }
    
    //檢查AB數量
    public static int[] checkAB(int[] input, int[] ans) {
        int inputC[] = input.clone();
        int ansC[] = ans.clone();
        int abNum[] = new int[2];
        abNum[0] = 0;   //A數量
        abNum[1] = 0;   //B數量
        //檢查A
        for (int i = 0; i < inputC.length; i++) {
            if (inputC[i] == ansC[i]) {
                abNum[0]++;
                ansC[i] = -1;   //檢查過的填-1 以免重複檢查
            }
        }
        //檢查B
        for (int i = 0; i < inputC.length; i++) {
            for (int n = 0; n < inputC.length; n++) {
                if(ansC[i] == -1) break;
                if (inputC[i] == ansC[n]) {
                    abNum[1]++;
                    ansC[n] = -2;    //檢查過的填-2 以免重複檢查
                    break;
                }
            }
        }
        return abNum;
    }
}

 

 

第二種修正寫法:

import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;

import java.util.Arrays;
import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String question = "";
        int count = 0;  //己算猜的次數
        for (int i = 0; i < 4; i++) {          //製作題目
            question += (int) (Math.random() * 10);     //字串可串接
        }
//        System.out.println(question);

        while (true) {
            System.out.print("請輸入數字:");
            String ans = sc.nextLine();
            int A = 0, B = 0;
            boolean checked[] = new boolean[4];
            Arrays.fill(checked, false);


            //檢查A
            for (int i = 0; i < ans.length(); i++) {
                if (question.charAt(i) == ans.charAt(i)) {
                    A++;
                    checked[i] = true;  //true代表檢查過了
                }
            }
            //檢查B
            for (int i = 0; i < ans.length(); i++) {
//                if(checked[i]) break; --------->這行不可以有,否則檢查過的位數,後面對不到,會少檢查
                for(int n = 0; n<ans.length(); n++){
                    if(!checked[n] && ans.charAt(i) == question.charAt(n)){
                        //true代表question裡的該位數字已經有對應到的ans數字了,不要再重複檢查
                        B++;
                        checked[n] = true;
                        break;
                    }
                }
            }
            count++;
            
            if(A==4){
                System.out.println("WIN!!!" + "猜了" + (count+1) +"");
                break;
            }
            System.out.println(A + "A" + B + "B");


        }


    }

}