前几天我问了一个关于我的代码的问题,很快就被这个令人难以置信的社区解决了。但是,我使用重写的代码版本遇到了一个完全独立的问题。这是我上一篇文章中对该计划的描述。
我试图编写一个程序来检测可以使用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键&#34;完成&#34;等结束了。 或者只需输入&#34;完成&#34;使用默认列表。
完成//提示使用默认列表
输入目标号码
15 //用户输入目标号码
0 0 0 0 0 0 ... //等等
答案 0 :(得分:3)
target = input.nextInt();
应该在你的循环中,否则变量永远不会改变
while(result<target)
永远不会变为虚假
while(result<target) {
target = input.nextInt();
// otherCoolCode
}
答案 1 :(得分:2)
您在while循环之前分配target
,并且您不会在while循环内以任何方式更改target
。您需要在while循环中提示用户。否则,如果将target
变量设置为大于0,则它将是无限循环。
答案 2 :(得分:2)
问题在于
if (last.get(firstIndex) + last.get(secondIndex) < result) {
...
}
结果始终初始化为零,因此该条件永远不会为真。 一种可能的解决方法是添加一个额外的条件来处理这个初始情况:
if (result == 0 || last.get(firstIndex) + last.get(secondIndex) < result) {
...
}
答案 3 :(得分:2)
如果循环中的条件在循环迭代期间不发生变化,则会发生无限循环。
让我们回顾一下你的循环:
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
为假。
在您的循环中,仅当result
为真时才分配给(last.get(firstIndex) + last.get(secondIndex) < result)
。因此,如果该条件为假,则结果不会改变。
您有一些其他状态不在循环条件本身但由循环操纵:firstIndex
和secondIndex
。您分配给它们的循环的每次迭代。你确实有一个&#39; else&#39;在打印当前值secondIndex
, 然而 之前修改result
的子句,然后立即在循环顶部分配给它。
这是你的无限循环的关键(当last.get(firstIndex) + last.get(secondIndex) < result
为假时):
result
无法更改last
未被修改,因此last.size()-1
和firstIndex - 1
保持不变secondIndex = firstIndex - 1;
覆盖循环结束时的减量,因此firstIndex
和secondIndex
都不会改变答案 4 :(得分:1)
在这一行
if (last.get(firstIndex) + last.get(secondIndex) < result) {
你的两个值的总和几乎不会小于结果,即&#34; 0&#34;。