Java 如何在填充了5个随机值的表格中搜索特定值&;1表示0的值

Java 如何在填充了5个随机值的表格中搜索特定值&;1表示0的值,java,Java,我需要编写一个程序来循环遍历每个城市的人口,如果它与一个人口为零的城市相邻(右或左),使其失去一半的人口,直到所有城市都没有剩下人口 示例输出: Day 0 [3, 6, 0, 4, 3, 2, 7, 0] Day 1 [3, 3, 0, 2, 3, 2, 3, 0] Day 2 [3, 1, 0, 1, 3, 2, 1, 0] Day 3 [3, 0, 0, 0, 3, 2, 0, 0] Day 4 [1, 0, 0, 0, 1, 1, 0, 0] Day 5 [0, 0, 0, 0, 0,

我需要编写一个程序来循环遍历每个城市的人口,如果它与一个人口为零的城市相邻(右或左),使其失去一半的人口,直到所有城市都没有剩下人口

示例输出:

Day 0 [3, 6, 0, 4, 3, 2, 7, 0]
Day 1 [3, 3, 0, 2, 3, 2, 3, 0]
Day 2 [3, 1, 0, 1, 3, 2, 1, 0]
Day 3 [3, 0, 0, 0, 3, 2, 0, 0]
Day 4 [1, 0, 0, 0, 1, 1, 0, 0]
Day 5 [0, 0, 0, 0, 0, 0, 0, 0]
---- EXTINCT ----
以下是我到目前为止的情况

package PROJECT;

    public class Main {
        public static void main(String[] args) {

            int[] day1 = {10, 6, 0, 4, 8, 10};
            System.out.println(day1);
            int[] day2 = {10, 3, 0, 2, 8, 10};    
            System.out.println(day2);
            int[] day3 = {10, 1, 0, 1, 8, 10};
            System.out.println(day3);


    while(day1[2] == 0){
                int day2[] = {10, 3, 0, 2, 4, 8, 10 };

            if(day2[1] == 0){
                int day3[]={5, 1, 0, 1, 4, 8, 10};
            } else if(day2[3]==0){
                int day3[]={10, 1, 0, 0, 2, 8, 10};
            }
            if(day3[1] == 0){
                int day4[] = {5,0,0,0,1,8,10};  
            } else if(day3[3]==0){
                int day4[]={5, 0, 0, 0, 0, 4, 10};
            }
            if(day4[1] == 0){
                int day5[] = {2,0,0,0,0,4,5};   
            } else if(day4[5]==0){
                int day4[]={2, 0, 0, 0, 0, 2, 10};
                }
    }
    }
    }

以下是一种适合您的方法:

public class ApocalypseNow {

    public static void main(String[] args) {
        int[] population = { 10, 6, 0, 4, 8, 10 };
        int currentValue;
        int diminishedValue;
        int dayCounters = 0;
        while (!areAllDead(population)) {
            for (int i = 0; i < population.length; i++) {
                if (checkNeighbours(i, population)) {
                    currentValue = population[i];
                    diminishedValue = currentValue / 2;
                    population[i] = diminishedValue;
                }
            }

            dayCounters++;
            System.out.print("Day " + dayCounters + ": ");
            printPopulation(population);
        }

    }

    public static boolean checkNeighbours(int currentIndex, int[] population) {
        int leftValue;
        int rightValue;
        try {
            leftValue = population[currentIndex - 1];
        } catch (ArrayIndexOutOfBoundsException e) {
            leftValue = -1;
        }
        try {
            rightValue = population[currentIndex + 1];
        } catch (ArrayIndexOutOfBoundsException e) {
            rightValue = -1;
        }

        if (leftValue == 0 || rightValue == 0)
            return true;
        return false;
    }

    public static void printPopulation(int[] population) {
        for (int i : population) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static boolean areAllDead(int[] population) {
        for (int i = 0; i < population.length; i++) {
            if (population[i] != 0)
                return false;
        }
        return true;
    }

}

为什么要在if/else中创建新数组?一旦您离开范围,它们将不再存在。您应该查找2D数组或数组列表,以便维护日常数组。然后循环数组,检查每个相邻单元格的0、检查边界等等,如果相邻单元格为0,则为每个找到的相邻0将数字除以2。我必须使用1-D数组和while/for循环。谢谢您的回答!
Day 1: 10 3 0 2 8 10 
Day 2: 10 1 0 1 8 10 
Day 3: 10 0 0 0 4 10 
Day 4: 5 0 0 0 2 10 
Day 5: 2 0 0 0 1 10 
Day 6: 1 0 0 0 0 5 
Day 7: 0 0 0 0 0 2 
Day 8: 0 0 0 0 0 1 
Day 9: 0 0 0 0 0 0