停止循环的条件

时间:2020-02-10 15:11:22

标签: java for-loop multidimensional-array

这个小程序并没有超出预期的结果。 看一看,是否可以帮忙!

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String numInput;

    // Declaring and initializing answer variable to an empty string.
    String answer = "";

    // Declaring and initializing the 2d array "number".
    String number[][] = {
            { "10", "20", "30" },
            { "15", "25", "35" }, };

    System.out.print("\n");

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print("\t" + number[i][j] + " ");
        }
        System.out.println("\n");
    }

    boolean found = false;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print("\n\tEnter a Number : ");

            // Expected input 10 or 15
            numInput = input.nextLine();

            // number on first line, first column.
            if (numInput.equals(number[i][0])) {
                found = true;
                System.out.print("\n\tNumber " + numInput + " found");
                System.out.print(" on line " + i + " colum " + j); 
            } 

            if (!found) {
                    System.out.print("\n\tNumber not found");
            }
        }
    }
}

应该接受用户的输入并将其与数组中的数据进行比较(仅第一列)。但这仅适用于第一列中的第一个数字

2 个答案:

答案 0 :(得分:1)

那是因为您为数组中的每个数字输入了input.nextLine。 您应该在这两个循环之外填充numInput。 像这样:

Scanner input = new Scanner(System.in);

String numInput;

// Declaring and initializing answer variable to an empty string.

String answer = "";

// Declaring and initializing the 2d array "number".

String number[][] = {

        { "10", "20", "30" },

        { "15", "25", "35" }, };

System.out.print("\n");

for (int i = 0; i < 2; i++) {

    for (int j = 0; j < 3; j++) {

        System.out.print("\t" + number[i][j] + " ");
    }

    System.out.println("\n");
}

boolean found = false;
System.out.print("\n\tEnter a Number : ");
numInput = input.nextLine();

for (int i = 0; i < 2; i++) {

    for (int j = 0; j < 3; j++) {            

        // number on first line, first column.

        if (numInput.equals(number[i][0])) {

            found = true;

            System.out.print("\n\tNumber " + numInput + " found");

            System.out.print(" on line " + i + " colum " + j);


            } 

        if (!found) {

                System.out.print("\n\tNumber not found");

        }
    }
}
}

答案 1 :(得分:1)

我希望代码中的注释使我的观点清楚,您在检查静态列索引时遍历Array,因此尽管j循环无济于事,更不用说您将输入设置为每个j循环==>会检查(当i = 0时)是否使用3个不同的输入输入了3次输入[0] [0]

    boolean found = false;
    //Input needs to be moved here so you don't iterate though it for each column.
    System.out.print("\n\tEnter a Number : ");
    numInput = input.nextLine();
    for (int i = 0; i < 2; i++) {

            //for (int j = 0; j < 3; j++) {    You cant determine if you found it in the column J because your are checking for [i][0] therefore the for loop for J is useless as it would match regardless of what number j is
            // number on first line, first column.
            if (numInput.equals(number[i][0])) {
                found = true;
                System.out.print("\n\tNumber " + numInput + " found");
                System.out.print(" on line " + i + " colum 0");
            }
            if (!found) {

                System.out.print("\n\tNumber not found");

            }
        //}
    }
相关问题