嵌套for循环

时间:2011-10-27 06:31:32

标签: java loops for-loop nested

我对常规for循环有一个不错的初学者理解但是我在Java中嵌套for循环时遇到了麻烦。

在我正在处理的问题中,我有一个最大数字的常数整数,然后我向用户询问4个不同的数字输入。从这4个输入中,我正在尝试确定哪些输入可以在我声明的常量整数内部。

IE:如果常数为30且用户输入5,9,3和21,它会告诉他们只能使用5,9和3,因为21太大而无法添加。

故事形式的问题是,用户有一个背包,可以承受一定的重量。该程序要求用户输入4个不同的项目权重,然后决定它可以放入包中的哪些项目。

这是针对学校项目的,所以我需要使用嵌套for循环。

4 个答案:

答案 0 :(得分:1)

任何想到嵌套for循环的简单方法是忽略它们是嵌套的这一事实。 按照惯例,通常使用i作为外循环的增量计数器,使用j作为内循环,这是在开始时保持笔直的最重要的事情。如果这对您来说是一个混乱点,那么为增量变量使用比字母“i”和“j”更具描述性的名称可能会有好处,例如outerinner。< / p>

在您尝试构建程序逻辑的任何特定时间,您只需要专注于您最直接在其中工作的for循环 - 至少在您开始并了解它们时第一次。

答案 1 :(得分:1)

我没有做任何JAVA,但我知道C#几乎是一样的。

我会这样做:

int max = 30;
int value = 0;
int counter = 0;
int[] input[4] = new int[5, 9, 3, 21];
bool[] canAddInput[4] = new bool[false, false, false, false];

for(value; value <= max; )
{
    for(counter; counter < 4; counter++)
    {
         value += input[i];
         if(value<=max)
             canAddInput[i] = true;
    }

    if(counter >= 4)
        Break;
}

答案 2 :(得分:0)

要理解嵌套循环,您可以从简单的示例开始,然后尝试更难的示例。例如,假设您想要制作一个计数器。

int i, j;
for (i=0; i <= 9; i++)
{
    for (j=0; j <= 9; j++)
    {
        System.out.println(i+""+j)
    }
}

输出是00到99之间的数字。您可以用纸或其他东西写出循环的输出,看看它是如何工作的。 让我们以这个循环为例,你有这个输出:

00 //here your program entered the outer loop, i has now the value 0, after that, you enter to the inner loop, i remains 0, but j will change in the next iteration
01 // you are still in the first iteration of the outer loop, but the inner loop is on the second
02 // and so on ....
03
04
05
06
07
08
09 // ... until the inner loop finished looping
10 // once the inner loop finished looping, the outer loop changes again, and we are back to the inner loop

一旦你清楚了解,你就可以决定嵌套循环的样子。外循环中需要使用哪些变量,以及内循环的变量。

答案 3 :(得分:0)

package com.examplehub.basics;

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

        /*
         * ####
         * ####
         * ####
         */
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 4; j++) {
                System.out.print("#");
            }
            System.out.println("\n");
        }

        /*
         * Outer loop iteration 1
         * i = 1; j = 1
         * i = 1; j = 2
         * i = 1; j = 3
         * i = 1; j = 4
         * Outer loop iteration 2
         * i = 2; j = 1
         * i = 2; j = 2
         * i = 2; j = 3
         * i = 2; j = 4
         * Outer loop iteration 3
         * i = 3; j = 1
         * i = 3; j = 2
         * i = 3; j = 3
         * i = 3; j = 4
         */
        for (int i = 1; i <= 3; ++i) {

            System.out.println("Outer loop iteration " + i);

            for (int j = 1; j <= 4; ++j) {
                System.out.println("i = " + i + "; j = " + j);
            }
        }

        /*
         * 1
         * 12
         * 123
         * 1234
         * 12345
         */
        for (int i = 1; i <= 5; ++i) {
            for (int j = 1; j <= i; j++) {
                System.out.print("" + j);
            }
            System.out.println();
        }

        /*
         * 1*1=1
         * 1*2=2    2*2=4
         * 1*3=3    2*3=6   3*3=9
         * 1*4=4    2*4=8   3*4=12  4*4=16
         * 1*5=5    2*5=10  3*5=15  4*5=20  5*5=25
         * 1*6=6    2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
         * 1*7=7    2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
         * 1*8=8    2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
         * 1*9=9    2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81
         */
        for (int i = 1; i <= 9; ++i) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + j * i + "\t");
            }
            System.out.println();
        }

        /*
         * A
         * AB
         * ABC
         * ABCD
         * ABCDE
         */
        for (int i = 1; i <= 5; i++) {
            char letter = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(letter++);
            }
            System.out.println();
        }

        /*
         * 1 2 3
         * 4 5 6
         * 7 8 9
         */
        int[][] array = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

        /*
         * 1 2 3
         * 4 5 6
         * 7 8 9
         */
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}

source