为什么我得到一个 0 的无限循环?(爪哇)



几天我问了一个关于我的代码的问题,这个问题很快就被这里令人难以置信的社区解决了。但是,我在使用重写版本的代码时遇到了一个完全独立的问题。这是我上一篇文章中对该程序的描述。

我正在尝试编写一个程序,该程序可以检测 ArrayList 中任何数字子集可以产生的最大总和,并且总和必须小于用户输入的目标数字。到目前为止,我的程序运行良好,除了一行(没有双关语)。请记住,此代码也尚未完成。

我现在对代码的问题是,在用户输入目标数字后,程序输出一个无限循环的 0。即使尝试调试,我仍然遇到问题。ArrayList 可以很好地应用于程序,但我认为我的一个while循环中的某个地方可能有问题。有什么想法吗?

这是代码。

import java.util.*;
class Sum{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int temp = 0, target = 1, result = 0, firstIndex = 0, secondIndex = 0;
        String tempString = null;
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Integer> last = new ArrayList<Integer>();
        System.out.println("Enter integers one at a time, pressing enter after each integer Type "done" when finished.nOR just type "done" to use the default list.");
        String placehold = "NotDone";
        while (!placehold.equals("done")){
            list.add(input.nextLine());
            placehold = list.get(list.size() - 1);
        }
        list.remove(list.size() - 1);
        if (list.size() == 0){            //Inserts default list if said list is empty
            list.add("1");
            list.add("2");
            list.add("4");
            list.add("5");
            list.add("8");
            list.add("12");
            list.add("15");
            list.add("21");
        }
        for (int i = 0; i < list.size(); i++){
            tempString = list.get(i);
            temp = Integer.parseInt(tempString);     //Changes the items in the list to Integers, which can be inserted into another list and then sorted
            last.add(temp);
        }
        Collections.sort(last);
        System.out.println("Enter the target number");
        target = input.nextInt();
        while (result < target){
            firstIndex = last.size() - 1;
            secondIndex = firstIndex - 1;
            while (last.get(firstIndex) > target){
                firstIndex--;
            }
            if (last.get(firstIndex) + last.get(secondIndex) < result){
                result = last.get(firstIndex) + last.get(secondIndex);
                last.remove(firstIndex);
                last.remove(secondIndex);
                last.add(result);
            }
            else{
                secondIndex--;
            }
            System.out.println(result);
        }  
    }
}

和输出...

一次输入一个整数,完成后在每个整数类型"完成"后按 Enter 键。 或者只需键入"完成"以使用默认列表。

完成//提示使用默认列表

输入目标号码

15//用户输入目标编号

0 0 0 0 0 0 ...//等等

target = input.nextInt();

应该在你的循环中,否则变量永远不会改变,并且

while(result<target)永远不会变成虚假


while(result<target) {
    target = input.nextInt();
    // otherCoolCode
}
你在 while 循环

之前分配target,并且你不会在 while 循环内以任何方式改变target。您需要在 while 循环中提示用户。否则,如果将target变量设置为高于 0,它将是一个无限循环。

问题出在

if (last.get(firstIndex) + last.get(secondIndex) < result) {
    ...
}

结果始终初始化为零,因此该条件永远不会为真。一种可能的解决方法是添加一个额外的条件来处理此初始情况:

if (result == 0 || last.get(firstIndex) + last.get(secondIndex) < result) {
    ...
}

当您有一个循环,其条件在循环迭代期间没有更改时,就会发生无限循环。

让我们回顾一下您的循环:

   while (result < target){
        firstIndex = last.size() - 1;
        secondIndex = firstIndex - 1;
        while (last.get(firstIndex) > target){
            firstIndex--;
        }
        if (last.get(firstIndex) + last.get(secondIndex) < result){
            result = last.get(firstIndex) + last.get(secondIndex);
            last.remove(firstIndex);
            last.remove(secondIndex);
            last.add(result);
        }
        else{
            secondIndex--;
        }
        System.out.println(result);
    }  

只有当result和/或target发生变化时,您的循环才会结束,因此result < target为假。

在你的循环中,只有当(last.get(firstIndex) + last.get(secondIndex) < result)为真时,你才分配给result。 因此,如果该条件为假,则结果不会改变。

您有一些其他状态不在循环条件本身中,而是由循环操作的:firstIndexsecondIndex 。 您分配给它们的循环的每次迭代。 你确实有一个"else"子句,您可以在打印result的当前值之前修改secondIndex但是您立即在循环的顶部分配给它。

这是无限循环的关键(当last.get(firstIndex) + last.get(secondIndex) < result为假时):

  1. result不会改变
  2. 您的列表last不会修改,因此last.size()-1firstIndex - 1保持不变
  3. 您分配secondIndex = firstIndex - 1;覆盖循环结束时的递减,因此既不会firstIndex也不会更改secondIndex

在这一行中

if (last.get(firstIndex) + last.get(secondIndex) < result) {

两个值的总和几乎永远不会小于结果,即"0"。

最新更新