【发布时间】:2019-07-03 04:09:08
【问题描述】:
我正在尝试编写一个程序,要求用户首先输入一个名称(字符串),然后输入一个数量(双精度)并将输入放入 2 个单独的数组列表中。这是通过一个while循环完成的。用户完成后,他们可以按 0,然后程序将打印两个数组列表。 第一次循环很完美,第二次它将打印要求输入的两行,而不允许在两者之间输入。当第二次给定输入时,它将显示 InputMismacchtException。
Scanner userInput = new Scanner(System.in);
ArrayList<String> customerName = new ArrayList<>();
ArrayList<Double> customerSpend = new ArrayList<>();
double checkUserInput = 1;
while (checkUserInput != 0) {
System.out.println("Please enter the customer name");
customerName.add(userInput.nextLine());
System.out.println("Please enter the customer amount");
customerSpend.add(userInput.nextDouble());
if (customerSpend.get(customerSpend.size()-1) == 0){
checkUserInput = 0;
}
}
for (int i = 0; i < customerName.size(); i++) {
System.out.println(customerName.get(i)+customerSpend.get(i));
}
【问题讨论】:
标签: java arraylist while-loop user-input