Java 嵌套循环中的计算错误

Java 嵌套循环中的计算错误,java,Java,代码的目标是找到与targetNumber相加的数字。例如,如果targetNumber=9,则该代码应获取数字的前两个出现索引,这两个索引相加为targetNumber。当我运行代码时,输出如下所示: The indexes are 10 and 1 代码的逻辑有什么问题?提前谢谢 public class TwoSum { public static void main(String[] args){ int[] myArray = {1, 6, 43, 22,

代码的目标是找到与
targetNumber
相加的数字。例如,如果
targetNumber=9
,则该代码应获取数字的前两个出现索引,这两个索引相加为
targetNumber
。当我运行代码时,输出如下所示:

The indexes are 10 and 1
代码的逻辑有什么问题?提前谢谢

public class TwoSum {

    public static void main(String[] args){

        int[] myArray = {1, 6, 43, 22, 4, 6, 4, 3, 8, 7, 3};
        int targetNumber = 9;
        int index1 = 0;;
        int index2 = 0;

        for(int i = 0; i < myArray.length; i++){
            for(int j = 1; j < myArray.length; j++){
                if(myArray[i] + myArray[j] == targetNumber){
                    index1 = i;
                    index2 = j;
                    break;
                }
            }
        }
        System.out.println("The indexes are " + index1 + " and " + index2);
    }
}
公共类TwoSum{
公共静态void main(字符串[]args){
int[]myArray={1,6,43,22,4,6,4,3,8,7,3};
int targetNumber=9;
int index1=0;;
int index2=0;
for(int i=0;i
中断时,只会中断内部循环,因此外部循环会继续,自然终止,并打印出索引10,然后打印出索引1,而不是打印出1和10


这样做的一个有趣结果是,您的代码基本上会找到最后一对数字,它们的总和为
targetNumber
,而不是第一对。如果让for循环倒计时而不是倒计时,代码应该输出正确的值,尽管这不是很有效…

当您中断时,您只中断内部循环,因此外部循环继续,自然终止,而不是打印出1和10,然后打印出索引10,然后打印出索引1


这样做的一个有趣结果是,您的代码基本上会找到最后一对数字,它们的总和为
targetNumber
,而不是第一对。如果让for循环倒计时而不是倒计时,代码应该输出正确的值,尽管这不是很有效…

break语句正在中断内部循环,而不是外部循环。所以最后的结果是3和6,而不是1和8

更合适的方法可能是:

    bool found=false;
    for(int i = 0; i < myArray.length; i++){
        if(!found){
            for(int j = 1; j < myArray.length; j++){
                if(myArray[i] + myArray[j] == targetNumber){ 
                    index1 = i; index2 = j;               
                    found=true; 
                    break; 
} } }}
boolfound=false;
for(int i=0;i
break语句正在中断内部循环,而不是外部循环。所以最后的结果是3和6,而不是1和8

更合适的方法可能是:

    bool found=false;
    for(int i = 0; i < myArray.length; i++){
        if(!found){
            for(int j = 1; j < myArray.length; j++){
                if(myArray[i] + myArray[j] == targetNumber){ 
                    index1 = i; index2 = j;               
                    found=true; 
                    break; 
} } }}
boolfound=false;
for(int i=0;i
我相信您期望的是索引0和8(值1和8)。问题是
break
语句只从内部循环中断,而不是从外部循环中断。您需要使用一个标志来知道您也应该从外部循环中断。如果没有找到匹配信息,也可以考虑打印消息。

public static void main(String[] args) throws Exception {
    int[] myArray = {1, 6, 43, 22, 4, 6, 4, 3, 8, 7, 3};
    int targetNumber = 9;
    int index1 = 0;
    int index2 = 0;

    boolean stop = false;
    for (int i = 0; i < myArray.length && !stop; i++) {
        for (int j = i + 1; j < myArray.length && !stop; j++) {
            if (myArray[i] + myArray[j] == targetNumber) {
                stop = true;
                index1 = i;
                index2 = j;
            }
        }
    }
    System.out.println(stop 
            ? "The indexes are " + index1 + " and " + index2
            : "No match found");
}
结果:

The indexes are 0 and 8

我相信您期望索引0和8(值1和8)。问题是
break
语句只从内部循环中断,而不是从外部循环中断。您需要使用一个标志来知道您也应该从外部循环中断。如果没有找到匹配信息,也可以考虑打印消息。

public static void main(String[] args) throws Exception {
    int[] myArray = {1, 6, 43, 22, 4, 6, 4, 3, 8, 7, 3};
    int targetNumber = 9;
    int index1 = 0;
    int index2 = 0;

    boolean stop = false;
    for (int i = 0; i < myArray.length && !stop; i++) {
        for (int j = i + 1; j < myArray.length && !stop; j++) {
            if (myArray[i] + myArray[j] == targetNumber) {
                stop = true;
                index1 = i;
                index2 = j;
            }
        }
    }
    System.out.println(stop 
            ? "The indexes are " + index1 + " and " + index2
            : "No match found");
}
结果:

The indexes are 0 and 8

您的代码有三个问题:

  • 根据规范,您希望找到第一个匹配索引,而 你实际上是最后一个。这是因为
    break
    只中断内部循环。简单的解决方法是使用标签(
    outer:
    )。尽管更干净的方法是使用专用方法进行搜索,并
    返回第一个匹配的值

  • 您将检查两次索引对,同时也检查同一索引对。消除这种冗余的惯用方法是从外部循环的索引的当前值(
    j=i
    )或(
    j=i+1
    )开始嵌套循环,如果您不想拥有相同的索引对(i,i)

  • 您没有考虑找不到匹配索引的情况。在本例中,您将显示(0,0)是结果

  • 以下是您的代码修复:

            public static class TwoSum {
    
                public static void main(String[] args) {
                    int[] myArray = {1, 6, 43, 22, 4, 6, 4, 3, 8, 7, 3};
                    int targetNumber = 9;
                    int index1 = -1;
                    int index2 = -1;
    
                    outer:
                    for (int i = 0; i < myArray.length; i++) {
                        for (int j = i + 1; j < myArray.length; j++) {
                            if (myArray[i] + myArray[j] == targetNumber) {
                                index1 = i;
                                index2 = j;
                                break outer;
                            }
                        }
                    }
    
                    if (index1 >= 0) {
                        System.out.println("The indexes are " + index1 + " and " + index2 + "(Values " + myArray[index1] +
                                " and " + myArray[index2] + ")");
                    } else {
                        System.out.println("Not found");
                    }
                }
            }
    
    公共静态类TwoSum{
    公共静态void main(字符串[]args){
    int[]myArray={1,6,43,22,4,6,4,3,8,7,3};
    int targetNumber=9;
    int index1=-1;
    int index2=-1;
    外部:
    for(int i=0;i=0){
    System.out.println(“索引为“+index1+”和“+index2+”(值“+myArray[index1]+
    和“+myArray[index2]+”);
    }否则{
    System.out.println(“未找到”);
    }
    }
    }
    
    您的代码有三个问题:

  • 根据规范,您希望找到第一个匹配索引,而 你实际上是最后一个。这是因为
    break
    只中断内部循环。简单的解决方法是使用标签(
    外部:
    The indexes are [{0, 8}, {1, 7}, {1, 10}, {5, 7}, {5, 10}, {7, 1}, {7, 5}, {10, 1}, {10, 5}]