Java 循环停止条件

Java 循环停止条件,java,for-loop,multidimensional-array,Java,For Loop,Multidimensional Array,这个小程序没有输出预期的结果。 请看一看,看你是否能帮忙 publicstaticvoidmain(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 字符串numInput; //将应答变量声明并初始化为空字符串。 字符串答案=”; //声明并初始化二维数组“number”。 字符串编号[][]={ { "10", "20", "30" }, { "15", "25", "35" }, }; 系统输出打印(“\n”); 对于(int i=0;i

这个小程序没有输出预期的结果。 请看一看,看你是否能帮忙

publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串numInput;
//将应答变量声明并初始化为空字符串。
字符串答案=”;
//声明并初始化二维数组“number”。
字符串编号[][]={
{ "10", "20", "30" },
{ "15", "25", "35" }, };
系统输出打印(“\n”);
对于(int i=0;i<2;i++){
对于(int j=0;j<3;j++){
系统输出打印(“\t”+数字[i][j]+”);
}
System.out.println(“\n”);
}
布尔值=false;
对于(int i=0;i<2;i++){
对于(int j=0;j<3;j++){
System.out.print(“\n\t输入一个数字:”);
//预期投入10或15
numInput=input.nextLine();
//第一行第一列的数字。
如果(numInput.等于(数字[i][0])){
发现=真;
System.out.print(“\n\t编号”+numInput+“找到”);
系统输出打印(“联机”+i+“列”+j);
} 
如果(!找到){
System.out.print(“\n\t找不到编号”);
}
}
}
}

它可以接受用户的输入,并将其与数组中的数据进行比较(仅第一列)。但它只对第一列中的第一个数字起作用,这是因为您对数组中的每个数字都输入.nextLine。 您应该在这两个循环之外填充numInput。 像这样:

扫描仪输入=新扫描仪(System.in);
字符串numInput;
//将应答变量声明并初始化为空字符串。
字符串答案=”;
//声明并初始化二维数组“number”。
字符串编号[][]={
{ "10", "20", "30" },
{ "15", "25", "35" }, };
系统输出打印(“\n”);
对于(int i=0;i<2;i++){
对于(int j=0;j<3;j++){
系统输出打印(“\t”+数字[i][j]+”);
}
System.out.println(“\n”);
}
布尔值=false;
System.out.print(“\n\t输入一个数字:”);
numInput=input.nextLine();
对于(int i=0;i<2;i++){
对于(int j=0;j<3;j++){
//第一行第一列的数字。
如果(numInput.等于(数字[i][0])){
发现=真;
System.out.print(“\n\t编号”+numInput+“找到”);
系统输出打印(“联机”+i+“列”+j);
} 
如果(!找到){
System.out.print(“\n\t找不到编号”);
}
}
}
}

我希望代码中的注释清楚地表明我的观点,即在检查静态列索引时迭代数组,因此运行j循环是无用的,更不用说设置输入每个j循环==>您将检查输入[0][0]是否使用3个不同的输入输入3次(当I=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");

            }
        //}
    }
boolean-found=false;
//需要将输入移到此处,这样您就不会对每列重复输入。
System.out.print(“\n\t输入一个数字:”);
numInput=input.nextLine();
对于(int i=0;i<2;i++){
//对于(int j=0;j<3;j++){您无法确定是否在列j中找到它,因为您正在检查[i][0],因此对于j的for循环是无用的,因为无论j是什么数字,它都将匹配
//第一行第一列的数字。
如果(numInput.等于(数字[i][0])){
发现=真;
System.out.print(“\n\t编号”+numInput+“找到”);
系统输出打印(“联机”+i+“列0”);
}
如果(!找到){
System.out.print(“\n\t找不到编号”);
}
//}
}

您说它
没有输出预期结果
。预期的结果是什么?你得到了什么?如果在第一列中输入了一个数字,它应该显示“找到的数字”以及它在哪一行和哪一列。否则“找不到号码”。但是它只在10分钟内完成,而不是15分钟(在第一列)。谢谢!但现在我需要它多次停止在“找到了数字”或“找不到数字”中显示。要解决这个问题,您还必须删除这两个循环之外的if语句,在本例中是在它们之后。
    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");

            }
        //}
    }