循环通过两个单独的if语句

时间:2022-10-19 13:47:11

The code below is a simplified version of a method I am working on for a java project. The method will sort through a list of items(two different categories), in this case 0,s and 1's. The code reads through an array of numbers stops at either 0 or 1 and then prints out both the 0 or one and the string of numbers following the 0 or 1. If a preceding string is a 1 or a zero then it will stop and switch to another if statement. However it only executes each statement once. However there is more in the array that it needs to read through and organize. I would like to set up some sort of loop so that it loops through the set of if statements until it has read through the entire array.

下面的代码是我正在为java项目工作的方法的简化版本。该方法将对项目列表(两个不同的类别)进行排序,在本例中为0,s和1。代码读取数字数组停止在0或1,然后打印出0或1以及0或1之后的数字字符串。如果前面的字符串是1或0,那么它将停止并切换到另一个if语句。但是它只执行一次每个语句。但是,阵列中还有更多需要通读和组织的内容。我想设置某种循环,以便循环遍历if语句集,直到它读完整个数组。

public class tester 
{

    public static void main(String[] args ) 
    {
        String flags[] = {"0","23","25","34","1","9","12","13","0","67","2","43"};
        String array[] = new String[flags.length];
        String zeros [] = new String[array.length];
        String ones[] = new String[array.length];

        int i,j,k,h;
        int count = 0;
        for (i = 0; i<flags.length; i++)
        {

            if (flags[i].equals("0")) 
            {       
                for (j=0; !flags[j].equals("1") ; j++)
                {
                    count = j+1;
                    array[j] = flags[j];
                    zeros[j] = flags[j];
                }   

            } else
                if (flags[count].equals("1"))
                {
                    j = 0;
                    for(k=count; !flags[k].equals("0");k++)
                    {
                        array[k] = flags[k];
                        j++;
                        ones[j-1] = flags[k];
                    }       
                }

        }

        for(i=0; i<zeros.length; i++)
        {System.out.println(zeros[i]);}

        System.out.println();

        for(i=0; i<ones.length; i++)
        {System.out.println(ones[i]);}
    }
}

What it prints out now:

现在打印出来的是什么:

0
23
25
34
null
null
null
null
null
null
null
null

1
9
12
13
null
null
null
null
null
null
null
null

1 个解决方案

#1


1  

    String flags[] = {"9","0","23","25","34","1","9","12","13","0","67","2","43"};
    String array[] = new String[flags.length];
    String zeros [] = new String[array.length];
    String ones[] = new String[array.length];

    int i;

    boolean addingZeroes = false;
    boolean addingOnes = false;
    int zeroCount = 0;
    int onesCount = 0;

    for (i = 0; i<flags.length; i++) {

        if (flags[i].equals("0")) {
            zeros[zeroCount] = flags[i];
            zeroCount++;
            addingZeroes = true;
            addingOnes = false;
        } else if (flags[i].equals("1")) {
            ones[onesCount] = flags[i];
            onesCount++;
            addingZeroes = false;
            addingOnes = true;
        } else if (addingZeroes) {
            zeros[zeroCount] = flags[i];
            zeroCount++;
        } else if (addingOnes) {
            ones[onesCount] = flags[i];
            onesCount++;
        }

    }

    for(i=0; i<zeroCount; i++) {
        System.out.println(zeros[i]);
    }

    System.out.println();

    for(i=0; i<onesCount; i++) {
        System.out.println(ones[i]);
    }

Hey, couple things were wrong. Basically, you need a little state machine where you need to know whether you are in the midst of storing the sequence after a 1 or a 0. I used the boolean values (eg addingZeroes) for that. Then, you need to separately keep track of your element count (eg zeroCount) for each of the storage arrays. You might have 20 digits after a 0 and just 2 after a 1. Finally, at the end, your length of your storage arrays is not what you want - you want the amount of values you ended up storing. That's why you got all those "nulls". One other thing I noticed is that your j value is initialized always to 0 in the 0 block, so you would always be using the lowest values of the start array.

嘿,几件事都错了。基本上,你需要一个小型状态机,你需要知道你是否正在将序列存储在1或0之后。我使用了布尔值(例如,添加了零)。然后,您需要单独跟踪每个存储阵列的元素数(例如,zeroCount)。在0之后可能有20位数,在1之后只有2位。最后,最后,您的存储阵列的长度不是您想要的 - 您想要最终存储的值的数量。这就是为什么你得到所有那些“空”。我注意到的另一件事是你的j值在0块中始终初始化为0,因此你总是使用start数组的最低值。

#1


1  

    String flags[] = {"9","0","23","25","34","1","9","12","13","0","67","2","43"};
    String array[] = new String[flags.length];
    String zeros [] = new String[array.length];
    String ones[] = new String[array.length];

    int i;

    boolean addingZeroes = false;
    boolean addingOnes = false;
    int zeroCount = 0;
    int onesCount = 0;

    for (i = 0; i<flags.length; i++) {

        if (flags[i].equals("0")) {
            zeros[zeroCount] = flags[i];
            zeroCount++;
            addingZeroes = true;
            addingOnes = false;
        } else if (flags[i].equals("1")) {
            ones[onesCount] = flags[i];
            onesCount++;
            addingZeroes = false;
            addingOnes = true;
        } else if (addingZeroes) {
            zeros[zeroCount] = flags[i];
            zeroCount++;
        } else if (addingOnes) {
            ones[onesCount] = flags[i];
            onesCount++;
        }

    }

    for(i=0; i<zeroCount; i++) {
        System.out.println(zeros[i]);
    }

    System.out.println();

    for(i=0; i<onesCount; i++) {
        System.out.println(ones[i]);
    }

Hey, couple things were wrong. Basically, you need a little state machine where you need to know whether you are in the midst of storing the sequence after a 1 or a 0. I used the boolean values (eg addingZeroes) for that. Then, you need to separately keep track of your element count (eg zeroCount) for each of the storage arrays. You might have 20 digits after a 0 and just 2 after a 1. Finally, at the end, your length of your storage arrays is not what you want - you want the amount of values you ended up storing. That's why you got all those "nulls". One other thing I noticed is that your j value is initialized always to 0 in the 0 block, so you would always be using the lowest values of the start array.

嘿,几件事都错了。基本上,你需要一个小型状态机,你需要知道你是否正在将序列存储在1或0之后。我使用了布尔值(例如,添加了零)。然后,您需要单独跟踪每个存储阵列的元素数(例如,zeroCount)。在0之后可能有20位数,在1之后只有2位。最后,最后,您的存储阵列的长度不是您想要的 - 您想要最终存储的值的数量。这就是为什么你得到所有那些“空”。我注意到的另一件事是你的j值在0块中始终初始化为0,因此你总是使用start数组的最低值。