我试图编写一个程序来检测可以使用ArrayList中任何数字子集进行的最大总和,并且总和必须低于用户输入的目标数。到目前为止,我的程序运行良好,除了一行(没有双关语)。请注意,此代码也尚未完成。
出于某种原因,在调用我的扫描仪时,我在下面标记的行上不断出现异常的InputMismatchException。当我运行程序并要求它应用默认列表时(正如您将在代码中看到的那样),我得到一个不寻常的输出
Enter integers (one at a time) to use, then type "done"
OR
You can just type "done" to use the default set
done //User Input here
Done
Enter your target number
在此行之后,抛出异常并且我被踢出程序(在BlueJ中编写并运行)。你们认为你可以帮助我吗?我假设它只是一个"语法"错误,如果你知道我的意思。代码如下:
import java.util.*;
/** This program will provide the largest sum of numbers within a set that is less than a user-implied target number. This is done by asking for a user input of integers (or none, for a default set), sorting the list, and calulating the highest sum starting with the largest number and moving downward on the set
@param input A Scanner used to detect user input
@param user A placeholder for user-implied integers
@param target The user-implied target number
@param index A reference to the currently "targeted" index on "list"
@param splitIndex Used if the list needs to be checked in non-adjacent indexes
@param finalOutput The final product of this program
@param list An ArrayList of Integers used as the set*/
class LargestSum{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int user = 0, target = 0, index = 0, finalOutput = 0, splitIndex = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("Enter integers (one at a time) to use, then type \"done\" \nOR\n You can just type \"done\" to use the default set");
/** A try-catch block is used to detect when the user has finished ading integers to the set*/
try{
user = input.nextInt();
list.add(user);
}
catch(InputMismatchException e){
System.out.print("Done");
if (list.size() == 0){
list.add(1);
list.add(2);
list.add(4);
list.add(5);
list.add(8);
list.add(12);
list.add(15);
list.add(21);
}
if (list.size() > 0){
Collections.sort(list);
System.out.println("\nEnter your target number");
target = input.nextInt(); //EXCEPTION IS THROWN HERE
index = list.size() - 1;
while (list.get(index) > target){
list.remove(index);
index = index - 1;
if (index == -1){
System.out.println("No sum can be made that is lower than the target number");
System.exit(0);
}
}
while (finalOutput < target){
if(list.get(index) + list.get(index - 1) < target){
finalOutput = list.get(index) + list.get(index - 1);
list.remove(index);
list.remove(index - 1);
list.add(finalOutput);
index = list.size() - 1;
}
else{
for (int i = index; i >= 0; i--){
if (list.get(i) + list.get(index) < target){
finalOutput = list.get(i) + list.get(index);
list.remove(i);
list.remove(index);
list.add(finalOutput);
index = list.size() - 1;
i = index;
}
}
}
}
}
System.out.println("Done!");
System.out.println("Largest Sum is: " + finalOutput);
}
}
}
谢谢大家,你可以忽略文档评论。正如我所说,该计划尚未完成,我计划增加更多。
〜AndrewM
答案 0 :(得分:0)
您需要致电 $('#panel').ready(function () {
$("#stbutton").click();
});
以允许扫描仪接收另一行输入。目前input.nextLine();
正在尝试将input.nextInt()
转换为导致Done
的{{1}},因为它使用您之前输入的相同输入行。
答案 1 :(得分:0)
问题出在这里 - user = input.nextInt(); 直到你通过nextInt()捕获整数,你的代码才行。但是当你提供&#34;完成&#34;时,你会得到inputMistmathexception。这个函数显然是有帮助的InputMismatchException.Hope。