为什么我会得到 0 的无限循环? (Java)
Why am I getting an infinite loop of 0's? (Java)
前几天我问了一个关于我的代码的问题,这个令人难以置信的社区很快就解决了。但是,我使用我的代码的重写版本遇到了一个完全不同的问题。这是我之前 post.
对程序的描述
I'm trying to write a program that can detect the largest sum that can be made with any subset of numbers in an ArrayList, and the sum must be lower than a user-input target number. My program is working flawlessly so far, with the exception of one line (no pun intended). Keep in mind that this code isn't complete yet too.
我现在的代码问题是,在用户输入目标数字后,程序输出一个无限循环的 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 integers one at a time, pressing enter after each integer Type "done" when finished.
OR just type "done" to use the default list.
done //Prompting to use the default list
Enter the target number
15 //User inputs target number
0
0
0
0
0
0
... //And so on
target = input.nextInt();
应该在你的循环中,否则变量永远不会改变并且
while(result<target)
永远不会变成false
while(result<target) {
target = input.nextInt();
// otherCoolCode
}
您在 while 循环之前分配了 target
,并且您没有在 while 循环内以任何方式更改 target
。您需要在 while 循环中提示用户。否则,如果你设置一个target
变量大于0,就会死循环。
这一行
if (last.get(firstIndex) + last.get(secondIndex) < result) {
两个值的总和几乎不会小于结果,即“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
and/ortarget
发生变化使得 result < target
为假时才会结束。
在您的循环中,只有当 (last.get(firstIndex) + last.get(secondIndex) < result)
为真时才分配给 result
。因此,如果该条件为假,则结果不会改变。
您有一些不在循环条件本身中但由循环操纵的附加状态:firstIndex
和 secondIndex
。您分配给它们的循环的每次迭代。您确实有一个 'else' 子句,您在打印 result
、however[=49 的当前值之前修改了 secondIndex
=] 然后你立即在循环的顶部分配给它。
这是无限循环的症结所在 (当 last.get(firstIndex) + last.get(secondIndex) < result
为假时):
result
不变
- 您的列表
last
未修改,因此 last.size()-1
和 firstIndex - 1
保持不变
- 您分配
secondIndex = firstIndex - 1;
覆盖循环结束时的减量,因此 firstIndex
和 secondIndex
都不会更改
前几天我问了一个关于我的代码的问题,这个令人难以置信的社区很快就解决了。但是,我使用我的代码的重写版本遇到了一个完全不同的问题。这是我之前 post.
对程序的描述I'm trying to write a program that can detect the largest sum that can be made with any subset of numbers in an ArrayList, and the sum must be lower than a user-input target number. My program is working flawlessly so far, with the exception of one line (no pun intended). Keep in mind that this code isn't complete yet too.
我现在的代码问题是,在用户输入目标数字后,程序输出一个无限循环的 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 integers one at a time, pressing enter after each integer Type "done" when finished. OR just type "done" to use the default list.
done //Prompting to use the default list
Enter the target number
15 //User inputs target number
0 0 0 0 0 0 ... //And so on
target = input.nextInt();
应该在你的循环中,否则变量永远不会改变并且
while(result<target)
永远不会变成false
while(result<target) {
target = input.nextInt();
// otherCoolCode
}
您在 while 循环之前分配了 target
,并且您没有在 while 循环内以任何方式更改 target
。您需要在 while 循环中提示用户。否则,如果你设置一个target
变量大于0,就会死循环。
这一行
if (last.get(firstIndex) + last.get(secondIndex) < result) {
两个值的总和几乎不会小于结果,即“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
and/ortarget
发生变化使得 result < target
为假时才会结束。
在您的循环中,只有当 (last.get(firstIndex) + last.get(secondIndex) < result)
为真时才分配给 result
。因此,如果该条件为假,则结果不会改变。
您有一些不在循环条件本身中但由循环操纵的附加状态:firstIndex
和 secondIndex
。您分配给它们的循环的每次迭代。您确实有一个 'else' 子句,您在打印 result
、however[=49 的当前值之前修改了 secondIndex
=] 然后你立即在循环的顶部分配给它。
这是无限循环的症结所在 (当 last.get(firstIndex) + last.get(secondIndex) < result
为假时):
result
不变- 您的列表
last
未修改,因此last.size()-1
和firstIndex - 1
保持不变 - 您分配
secondIndex = firstIndex - 1;
覆盖循环结束时的减量,因此firstIndex
和secondIndex
都不会更改