【发布时间】:2013-12-04 01:36:35
【问题描述】:
我正在使用一个 do-while 循环,该循环根据用户的输入作为“Y”或“N”进行循环。如果用户选择按“Y”或 1(为简单起见)继续,则在第二次迭代时,它将跳过同一循环中的第一个输入。
do{
System.out.println("Enter the location: ");
cartItem.purchaseLocation = input.nextLine(); // This input is skipped on second iteration
System.out.println("Enter the product description: ");
cartItem.productDescription = input.nextLine();
System.out.println("Enter the price of the item: ");
cartItem.productPrice = input.nextDouble();
System.out.println("Enter the quantity: ");
cartItem.quantity = input.nextInt();
cartList.add(cartItem);
System.out.println("Do you want to add more items ? y = 1 / n = 0: ");
exitVar = input.nextInt();
}while(exitVar!=0); // Repeat till no more items need to be added in the cart
所以当用户按下输入 0 重复该过程时,行
cartItem.purchaseLocation = input.nextLine();
将跳过循环中的第一个输入。有什么建议这里可能有什么问题吗?
【问题讨论】:
标签: java loops java.util.scanner do-while