无法弄清楚为什么程序将一个字符串分配给整个数组

时间:2014-11-24 03:13:52

标签: java arrays string for-loop iteration

我正在尝试用java创建24游戏来练习。 24 Game是一种算术游戏,其目标是找到一种操纵四个整数的方法,使得最终结果为24.可以使用任何数字顺序的加法,减法,乘法或除法来制作四位数操作从一到九等于24。

规则很简单:你只需要使用每个数字一次,只使用从用户那里读取的4个数字来找到一个方程式来获得24。

我的问题是我的程序正在为数组的每个元素分配相同的方程式。当它打印时,我得到“发现副本”并且打印相同的等式20次。找到的副本现在只是一个占位符。我希望程序将新字符串保存到数组中的下一个元素,如果它是一个副本,那么什么都不做。我想我可能在append方法的循环中有一些不正确的逻辑。但是,我无法理解。

public class TwentyFourGame {
    String [] ans = new String[20];
    boolean [] used = new boolean[ans.length];
    int count = 0;
    int value = 0;

    public void Solve(double w, double x, double y, double z){
        double [] nums = {w,x,y,z};
        double eHold=0, fHold=0;

        //Replaces null array elements with empty string to avoid NPE
        for(int d=0; d<ans.length;d++){
            ans[d] = "";
        }

        for (int a=0;a< 4; a++){
            for (int b=0;b< 3; b++){
                for (int c=0;c<2; c++){
                    fHold = nums[2]+nums[3];
                    if (((nums[0]/nums[1])*fHold)==24)
                        append (nums[0]+" / "+nums[1]+" * ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else if (((nums[0]/nums[1])-fHold)==24)
                        append (nums[0]+" / "+nums[1]+" - ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])-fHold)==24)
                        append (nums[0]+" * "+nums[1]+" - ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])/fHold)==24)
                        append (nums[0]+" * "+nums[1]+" / ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else if (((nums[0]-nums[1])*fHold)==24)
                        append (nums[0]+" - "+nums[1]+" * ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else if (((nums[0]-nums[1])/fHold)==24)
                        append (nums[0]+" - "+nums[1]+" / ("+nums[2]+" + "+nums[3]+")" + " = 24");
                    else value++;
                    fHold=nums[2]-nums[3];

                    if (((nums[0]/nums[1])*fHold)==24)
                        append (nums[0]+" / "+nums[1]+" * ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else if (((nums[0]/nums[1])+fHold)==24)
                        append (nums[0]+" / "+nums[1]+" + ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])+fHold)==24)
                        append (nums[0]+" * "+nums[1]+" + ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])/fHold)==24)
                        append (nums[0]+" * "+nums[1]+" / ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])*fHold)==24)
                        append (nums[0]+" + "+nums[1]+" * ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])/fHold)==24)
                        append (nums[0]+" + "+nums[1]+" / ("+nums[2]+" - "+nums[3]+")" + " = 24");
                    else value++;
                    fHold=nums[2]*nums[3];

                    if (((nums[0]/nums[1])-fHold)==24)
                        append (nums[0]+" / "+nums[1]+" * ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else if (((nums[0]/nums[1])+fHold)==24)
                        append (nums[0]+" / "+nums[1]+" + ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else if (((nums[0]-nums[1])+fHold)==24)
                        append (nums[0]+" - "+nums[1]+" + ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else if (((nums[0]-nums[1])/fHold)==24)
                        append (nums[0]+" - "+nums[1]+" / ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])-fHold)==24)
                        append (nums[0]+" + "+nums[1]+" - ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])/fHold)==24)
                        append (nums[0]+" + "+nums[1]+" / ("+nums[2]+" * "+nums[3]+")" + " = 24");
                    else value++;
                    fHold=nums[2]/nums[3];

                    if (((nums[0]-nums[1])*fHold)==24)
                        append (nums[0]+" - "+nums[1]+" * ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else if (((nums[0]-nums[1])+fHold)==24)
                        append (nums[0]+" - "+nums[1]+" + ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])+fHold)==24)
                        append (nums[0]+" * "+nums[1]+" + ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else if (((nums[0]*nums[1])-fHold)==24)
                        append (nums[0]+" * "+nums[1]+" - ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])*fHold)==24)
                        append (nums[0]+" + "+nums[1]+" * ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else if (((nums[0]+nums[1])-fHold)==24)
                        append (nums[0]+" + "+nums[1]+" - ("+nums[2]+" / "+nums[3]+")" + " = 24");
                    else value++;
                    fHold=nums[1]*nums[2];

                    if (((nums[0]-fHold)/nums[3])==24)
                        append (nums[0]+" - ("+nums[1]+" * "+nums[2]+") / "+nums[3] + " = 24");
                    else if (((nums[0]-fHold)+nums[3])==24)
                        append (nums[0]+" - ("+nums[1]+" * "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)+nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" * "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)-nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" * "+nums[2]+") - "+nums[3] + " = 24");
                    else if (((nums[0]+fHold)/nums[3])==24)
                        append (nums[0]+" + ("+nums[1]+" * "+nums[2]+") / "+nums[3] + " = 24");
                    else if (((nums[0]+fHold)-nums[3])==24)
                        append (nums[0]+" + ("+nums[1]+" * "+nums[2]+") - "+nums[3] + " = 24");
                    else value++;
                    fHold=nums[1]-nums[2];

                    if (((nums[0]*fHold)/nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" - "+nums[2]+") / "+nums[3] + " = 24");
                    else if (((nums[0]*fHold)+nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" - "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)+nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" - "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)*nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" - "+nums[2]+") * "+nums[3] + " = 24");
                    else value++;
                    fHold=nums[1]/nums[2];

                    if (((nums[0]-fHold)*nums[3])==24)
                        append (nums[0]+" - ("+nums[1]+" / "+nums[2]+") * "+nums[3] + " = 24");
                    else if (((nums[0]-fHold)+nums[3])==24)
                        append (nums[0]+" - ("+nums[1]+" / "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]*fHold)+nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" / "+nums[2]+") + "+nums[3] + " = 24");
                    else if (((nums[0]*fHold)-nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" / "+nums[2]+") - "+nums[3] + " = 24");
                    else if (((nums[0]+fHold)*nums[3])==24)
                        append (nums[0]+" + ("+nums[1]+" / "+nums[2]+") * "+nums[3] + " = 24");
                    else if (((nums[0]+fHold)-nums[3])==24)
                        append (nums[0]+" + ("+nums[1]+" / "+nums[2]+") - "+nums[3] + " = 24"); 
                    else value++;
                    fHold=nums[1]+nums[2];

                    if (((nums[0]*fHold)/nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" + "+nums[2]+") / "+nums[3] + " = 24");
                    else if (((nums[0]*fHold)-nums[3])==24)
                        append (nums[0]+" * ("+nums[1]+" + "+nums[2]+") - "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)-nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" + "+nums[2]+") - "+nums[3] + " = 24");
                    else if (((nums[0]/fHold)*nums[3])==24)
                        append (nums[0]+" / ("+nums[1]+" + "+nums[2]+") * "+nums[3] + " = 24");
                    else value++;

                    if (value == 192){
                        append("There is no solution for this set of numbers");

                        this.count ++;
                    }
                    // 0=a 1=b 2=c 3=d
                    eHold=nums[2];
                    nums[2]=nums[3];
                    nums[3]=eHold;
                }
                eHold=nums[1];
                nums[1]=nums[2];
                nums[2]=nums[3];
                nums[3]=eHold;
            }
            eHold=nums[0];
            nums[0]=nums[1];
            nums[1]=nums[2];
            nums[2]=nums[3];
            nums[3]=eHold;
        }

        //Only prints elements with non-empty strings
        for (int j=0; j<ans.length;j++){
            if (ans[j] != "")
                System.out.println(ans[j]);
        }
    }


    //Method used to check if there are duplicates in the array of equations
    public void append(String equation){
        String text = equation;

        //Check if equation is a copy
        Copy:
        for (int c=0;c<ans.length;c++){
            if (text.equals(ans[c])){
                System.out.println("Found copy");
                break Copy;
            }

            //If it's not then add it to the array
            else {
                Add:
                for (int e=0;e<ans.length;e++){
                    if (used[e]==false){
                        ans[e] = text;
                        used[e] = true;
                        break Add;
                    }
                }
            }
        }
    }


    public static void main(String[]args){
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.println("Enter 4 numbers between 1 - 9");
        double w,x,y,z;
        TwentyFourGame Player1 = new TwentyFourGame();
        w = input.nextDouble();
        x = input.nextDouble();
        y = input.nextDouble();
        z = input.nextDouble();
        input.close();
        Player1.Solve(w,x,y,z);
    }
}

0 个答案:

没有答案