无法弄清楚为什么我的程序不打印数组的内容

时间:2014-11-23 01:43:21

标签: java arrays for-loop

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

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

我的问题是程序完成后我没有收到任何输出。问题可能出在append(String equation)方法中,该方法用于防止重复的答案并将正确的方程式添加到ans数组中。或者它可能在其他地方。我无法理解我的生活。

编辑代码:

public class TwentyFourGame {

    String [] ans = new String[20];
    boolean [] used = new boolean[20];
    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;

            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;

        for (int e=0;e<used.length;e++){
            if (used[e]==false){
                ans[e] = text;
                used[e] = true;
                break;
            }
        }

        //Check if elements in the array are the same
        for (int c=0;c<ans.length;c++){
            for (int d = 0; d < ans.length; d++) {
                //if elements are the same do nothing
                if (ans[c].equals(ans[d])){
                    ans[d] = "";
                }
            }
        }
    }


    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);
    }
}

2 个答案:

答案 0 :(得分:0)

  1. 双输入?你确定你不想使用int吗?
  2. 您不会将任何结果打印到控制台中。只是尝试查找所有System.out.println(...)语句。

    int value = 0;
    
    if (value == 192){ // <== Here value == 0, so this condition is always FALSE. And print never happens.
        for (int i=0; i<ans.length;i++){
            System.out.println(ans[i]);
        }
    }
    

答案 1 :(得分:0)

我怀疑你的逻辑是错误的,这就是你永远不会使用append(String)方法的原因。如果你调试它,你会发现value只能达到32,而不是你所期望的192。

此外,一旦您点击append方法(尝试1,1,3,7),当您尝试评估NullPointerException时会得到ans[c].equals(ans[d]),因为{{1}是空的。