2d数组循环计数不能正常工作。

时间:2021-05-15 21:34:33

So this is pretty close to complete, here is the Output. My methods are working perfectly, so that's not the issue. The output will give the best explanation but basically I have to print a list of 4096 random integers and ask the users input for a number. With that number I have to tell the user how many loops it took to find it. The issue is that it is not counting properly. It skips numbers that are in the output and if it is not found it prints both of the if statements. I have already built this program for a 1d array, and I am unable to use an Arraylist. Thanks!

这很接近完成,这是输出。我的方法是完美的,所以这不是问题。输出将给出最好的解释,但基本上我必须打印一个4096个随机整数的列表,并要求用户输入一个数字。用这个数字,我必须告诉用户找到它需要多少个循环。问题是它没有正确计算。它跳过输出中的数字,如果没有发现,它会打印出if语句。我已经为1d阵列构建了这个程序,我不能使用Arraylist。谢谢!

Scannner s = new Scanner(System.in);

    int[][] input = new int[5][1];
    int[][] arrayone = new int[4097][1];
    int loop = 0;

for (int id = 0; id < input.length; id++) {
    for (int x = 0; x <input[id].length; x++) {
        System.out.println("Please enter a number between " + min + " and " + max);
        input[id][x] = s.nextInt();

        if (min <= input[id][x] && input[id][x] <= max) {
            for (int count = 0; count < arrayone.length; count++) {
                for (int count2 = 0; count2 < arrayone[count].length; count2++) {
                    if (arrayone[count][count2] != input[id][x]) {
                        loop++;
                    }
                    else {
                        break;
                    }
                }
            }
        if (input[id][x] != arrayone.length){
            System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
        }
        if(loop > 4096) {
            System.out.println(input[id][x] + " was not found");
        }
        loop = 0;
    }
}

Update:

更新:

I have used part of the code below to update my own and now the print is either not found or 4096, here is an Updated Output. Below is the changes I have made to the code according to suggestion:

我已经使用了下面的部分代码来更新我自己的,现在打印要么没有找到,要么是4096,这是一个更新的输出。下面是我根据建议对代码做出的修改:

if(loop > 4096) {
        System.out.println(input[id][x] + " was not found");
        }
        else{
        System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
        }
        loop = 0;

3 个解决方案

#1


0  

As mentioned in the comments, your code has some oddities that need addressing, especially the dimensions. This is disguising the fact you aren't breaking out of the outer loop to end the search process with your break statement, as an unlabeled break only breaks out of the innermost loop. Assuming you create a 2D array that has more than the one dimension, you can use a label to break out of the outer loop.

正如注释中提到的,您的代码有一些需要解决的奇怪问题,尤其是维度。这是在掩盖你没有打破外部循环,用你的break语句结束搜索过程的事实,因为没有标记的break只是从最里面的循环中跳出来的。假设您创建的2D数组具有多个维度,您可以使用一个标签来打破外部循环。

outerloop:
for (int count = 0; count < arrayone.length; count++) {
    for (int count2 = 0; count2 < arrayone[count].length; count2++) {
        loop++;    //assume first check counts as one loop
        if (arrayone[count][count2] == input[id][x]) {
            break outerloop;
        }
    }
}

For your second issue, you might need to correct the numbers as you fix the oddities in your program's code, but since your program only has two results (found or didn't find number), you can use a simple if/else statement to write only one output dialogue per search.

对于您的第二个问题,您可能需要修正程序代码中奇怪的数字,但是由于您的程序只有两个结果(发现或没有找到数字),您可以使用一个简单的if/else语句来每次搜索只写一个输出对话。

    if(loop > 4096) {
        System.out.println(input[id][x] + " was not found");
    }
    else{
        System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
    }

#2


0  

Firstly, it looks like you're missing a closing bracket for your outer for loop.

首先,它看起来像是你的外环缺少一个闭合支架。

Secondly, the problem is that break; only breaks out of the inner loop so the outer loop would still continue. One way to fix this is by using a boolean flag outside both loops and setting it to true when you want to break so the outer loop has some way of knowing it should break out now.

第二,问题在于休息;只有打破内部循环,这样外环才会继续。解决这个问题的一种方法是在两个循环外面使用一个布尔标志,并在你想要断开的时候将它设置为true,这样外部循环就可以知道它现在应该被打破了。

#3


0  

Thank you both so so much! I got it working! This is how I did it:

非常感谢你们两位!我明白了工作!我就是这样做的:

boolean foundit = false;         
   // if (min <= input[id][x] && input[id][x] <= max) {
        for (int count = 0; count < arrayone.length && !foundit; count++) {
            for (int count2 = 0; count2 < arrayone[count].length; count2++) {                 
            if (arrayone[count][count2] == input[id][x]) {
                foundit = true;
                break;
            }
            else {
                loop++;
            }
            }
        }

#1


0  

As mentioned in the comments, your code has some oddities that need addressing, especially the dimensions. This is disguising the fact you aren't breaking out of the outer loop to end the search process with your break statement, as an unlabeled break only breaks out of the innermost loop. Assuming you create a 2D array that has more than the one dimension, you can use a label to break out of the outer loop.

正如注释中提到的,您的代码有一些需要解决的奇怪问题,尤其是维度。这是在掩盖你没有打破外部循环,用你的break语句结束搜索过程的事实,因为没有标记的break只是从最里面的循环中跳出来的。假设您创建的2D数组具有多个维度,您可以使用一个标签来打破外部循环。

outerloop:
for (int count = 0; count < arrayone.length; count++) {
    for (int count2 = 0; count2 < arrayone[count].length; count2++) {
        loop++;    //assume first check counts as one loop
        if (arrayone[count][count2] == input[id][x]) {
            break outerloop;
        }
    }
}

For your second issue, you might need to correct the numbers as you fix the oddities in your program's code, but since your program only has two results (found or didn't find number), you can use a simple if/else statement to write only one output dialogue per search.

对于您的第二个问题,您可能需要修正程序代码中奇怪的数字,但是由于您的程序只有两个结果(发现或没有找到数字),您可以使用一个简单的if/else语句来每次搜索只写一个输出对话。

    if(loop > 4096) {
        System.out.println(input[id][x] + " was not found");
    }
    else{
        System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
    }

#2


0  

Firstly, it looks like you're missing a closing bracket for your outer for loop.

首先,它看起来像是你的外环缺少一个闭合支架。

Secondly, the problem is that break; only breaks out of the inner loop so the outer loop would still continue. One way to fix this is by using a boolean flag outside both loops and setting it to true when you want to break so the outer loop has some way of knowing it should break out now.

第二,问题在于休息;只有打破内部循环,这样外环才会继续。解决这个问题的一种方法是在两个循环外面使用一个布尔标志,并在你想要断开的时候将它设置为true,这样外部循环就可以知道它现在应该被打破了。

#3


0  

Thank you both so so much! I got it working! This is how I did it:

非常感谢你们两位!我明白了工作!我就是这样做的:

boolean foundit = false;         
   // if (min <= input[id][x] && input[id][x] <= max) {
        for (int count = 0; count < arrayone.length && !foundit; count++) {
            for (int count2 = 0; count2 < arrayone[count].length; count2++) {                 
            if (arrayone[count][count2] == input[id][x]) {
                foundit = true;
                break;
            }
            else {
                loop++;
            }
            }
        }