试图弄清楚为什么我的代码会继续打印

时间:2014-11-21 14:48:22

标签: java append stringbuffer

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

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

基本上我的问题是我试图将等式== 24附加到StringBuffer输出。我可能错误地使用StringBuffer。我想将所有方程式添加到StringBuffer中,然后在将StringBuffer打印到控制台之前删除所有重复的方程式。然而,无论所有方程打印什么,我都不确定为什么。我不确定StringBuffer是否可以实现这一点,因为它可能只附加字符。

int i = 0;
public void Solve(double w, double x, double y, double z){
    double [] nums = {w,x,y,z};
    int length = nums.length;
    double eHold=0, fHold=0;
    boolean [] used = new boolean[length];
    StringBuffer output = new StringBuffer(400);
    int position = 0;
    int value = 0;

    if (position == nums.length){
        //for (int i=0; i<output.length();i++){
            //if (output.)
            System.out.println("");
            return;
        //}
    }

    else {
        for (int i=0; i<nums.length; i++){

            if (((nums[i]/nums[i+1])*fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else if (((nums[i]/nums[i+1])-fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" - ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])-fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" - ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])/fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" / ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else if (((nums[i]-nums[i+1])*fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" * ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else if (((nums[i]-nums[i+1])/fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" / ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
            else value++;
            fHold=nums[i+3]-nums[i+4];

            if (((nums[i]/nums[i+1])*fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else if (((nums[i]/nums[i+1])+fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" + ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])+fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" + ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])/fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" / ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])*fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" * ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])/fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" / ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
            else value++;
            fHold=nums[i+3]*nums[i+4];

            if (((nums[i]/nums[i+1])-fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else if (((nums[i]/nums[i+1])+fHold)==24)
                output.append (nums[i]+" / "+nums[i+1]+" + ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else if (((nums[i]-nums[i+1])+fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" + ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else if (((nums[i]-nums[i+1])/fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" / ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])-fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" - ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])/fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" / ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
            else value++;
            fHold=nums[i+3]/nums[i+4];

            if (((nums[i]-nums[i+1])*fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" * ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else if (((nums[i]-nums[i+1])+fHold)==24)
                output.append (nums[i]+" - "+nums[i+1]+" + ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])+fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" + ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else if (((nums[i]*nums[i+1])-fHold)==24)
                output.append (nums[i]+" * "+nums[i+1]+" - ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])*fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" * ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else if (((nums[i]+nums[i+1])-fHold)==24)
                output.append (nums[i]+" + "+nums[i+1]+" - ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
            else value++;
            fHold=nums[i+1]*nums[i+3];

            if (((nums[i]-fHold)/nums[i+4])==24)
                output.append (nums[i]+" - ("+nums[i+1]+" * "+nums[i+3]+") / "+nums[i+4] + " = 24");
            else if (((nums[i]-fHold)+nums[i+4])==24)
                output.append (nums[i]+" - ("+nums[i+1]+" * "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)+nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" * "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)-nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" * "+nums[i+3]+") - "+nums[i+4] + " = 24");
            else if (((nums[i]+fHold)/nums[i+4])==24)
                output.append (nums[i]+" + ("+nums[i+1]+" * "+nums[i+3]+") / "+nums[i+4] + " = 24");
            else if (((nums[i]+fHold)-nums[i+4])==24)
                output.append (nums[i]+" + ("+nums[i+1]+" * "+nums[i+3]+") - "+nums[i+4] + " = 24");
            else value++;
            fHold=nums[i+1]-nums[i+3];

            if (((nums[i]*fHold)/nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" - "+nums[i+3]+") / "+nums[i+4] + " = 24");
            else if (((nums[i]*fHold)+nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" - "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)+nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" - "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)*nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" - "+nums[i+3]+") * "+nums[i+4] + " = 24");
            else value++;
            fHold=nums[i+1]/nums[i+3];

            if (((nums[i]-fHold)*nums[i+4])==24)
                output.append (nums[i]+" - ("+nums[i+1]+" / "+nums[i+3]+") * "+nums[i+4] + " = 24");
            else if (((nums[i]-fHold)+nums[i+4])==24)
                output.append (nums[i]+" - ("+nums[i+1]+" / "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]*fHold)+nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" / "+nums[i+3]+") + "+nums[i+4] + " = 24");
            else if (((nums[i]*fHold)-nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" / "+nums[i+3]+") - "+nums[i+4] + " = 24");
            else if (((nums[i]+fHold)*nums[i+4])==24)
                output.append (nums[i]+" + ("+nums[i+1]+" / "+nums[i+3]+") * "+nums[i+4] + " = 24");
            else if (((nums[i]+fHold)-nums[i+4])==24)
                output.append (nums[i]+" + ("+nums[i+1]+" / "+nums[i+3]+") - "+nums[i+4] + " = 24"); 
            else value++;
            fHold=nums[i+1]+nums[i+3];

            if (((nums[i]*fHold)/nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" + "+nums[i+3]+") / "+nums[i+4] + " = 24");
            else if (((nums[i]*fHold)-nums[i+4])==24)
                output.append (nums[i]+" * ("+nums[i+1]+" + "+nums[i+3]+") - "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)-nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" + "+nums[i+3]+") - "+nums[i+4] + " = 24");
            else if (((nums[i]/fHold)*nums[i+4])==24)
                output.append (nums[i]+" / ("+nums[i+1]+" + "+nums[i+3]+") * "+nums[i+4] + " = 24");
            else value++;

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

                this.i ++;
            }
            position ++;
            eHold=nums[i+3];
            nums[i+3]=nums[i+4];
            nums[i+4]=eHold;
        }
        position ++;
        eHold=nums[i+1];
        nums[i+1]=nums[i+3];
        nums[i+3]=nums[i+4];
        nums[i+4]=eHold;
    }
    position ++;
    eHold=nums[i];
    nums[i]=nums[i+1];
    nums[i+1]=nums[i+3];
    nums[i+3]=nums[i+4];
    nums[i+4]=eHold;

}


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;
    Game Player1 = new Game();
    w = input.nextDouble();
    x = input.nextDouble();
    y = input.nextDouble();
    z = input.nextDouble();
    input.close();
    Player1.Solve(w,x,y,z);
}

}

1 个答案:

答案 0 :(得分:0)

Stringbuffer并不能真正让你删除内容(确实如此,但不是逐行删除) 你可能最好创建一个添加线条的Set。请注意,这是无序的,可能并不理想,但它将保证您不会两次打印相同的字符串。