【发布时间】:2017-01-31 14:40:16
【问题描述】:
好的,所以我试图捕捉这些我可以做的用户输入,但问题是当我捕捉到它时,它不会返回到用户输入错误(InputMismatchException)的输入,而是会回到开始的循环。假设如果用户在第二个输入上出错,它将返回到用户正确输入的第一个输入。我只是将其保留为基本并删除了我的尝试。
public class TestRefuseTruck {
public static void main(String[] args) {
int maxBins;
int rate;
int weight;
int count = 0;
Scanner in = new Scanner(System.in);
try {
System.out.println("Enter the number of bins the truck can collect: ");
maxBins = in.nextInt();
System.out.println("Enter the cost per kilo:");
rate = in.nextInt();
RefuseTruck r = new RefuseTruck(maxBins, rate);
while (count < maxBins) {
System.out.println("Enter the weight for bin " + (count + 1));
weight = in.nextInt();
if (r.collectBin(weight) == true) {
count++;
}
}
r.printStats();
} catch (InputMismatchException e) {
System.out.println("Incorrect Input.");
}
}
}
【问题讨论】:
-
你的
try-catch在哪里? -
你的
RefuseTruck班级在哪里? -
@RajS.Rusia 这无关紧要。我只是想在测试类中捕获 inputmismatch 并在那里处理它。