每当我输入值时,java scanner总是跳过一行
这是我第一次在这里发帖。我对扫描仪的方法有问题。这是我正在使用的代码每当我输入值时,java scanner总是跳过一行,java,for-loop,arraylist,java.util.scanner,do-while,Java,For Loop,Arraylist,Java.util.scanner,Do While,这是我第一次在这里发帖。我对扫描仪的方法有问题。这是我正在使用的代码 public void loanBook() { Scanner input = new Scanner(System.in); boolean successful = false; do { System.out.println( "\nPlease enter the book ID of the book
public void loanBook() {
Scanner input = new Scanner(System.in);
boolean successful = false;
do {
System.out.println(
"\nPlease enter the book ID of the book that you wish to take out (Press 9 to exit to the main menu)");
if (input.nextInt() == 9) {
successful = true;
break;
}
int bookID = input.nextInt();
for (int i = 0; i < Books.size(); i++) {
if (Books.get(i).getBookID() == bookID) {
do {
System.out.println("\nHow long would you like to loan the book for (20 Days maximum):");
int durationOnLoan = input.nextInt();
if (durationOnLoan <= 20 && 1 <= durationOnLoan) {
Books.get(i).setDurationOnLoan(durationOnLoan);
successful = true;
} else {
System.out.println("The number of days you have entered is invalid");
}
} while (successful == false);
System.out.println("\nThe book " + Books.get(i).getTitle() + " is now on loan");
Books.get(i).setOnLoan(true);
Books.get(i).setNumOfLoan(Books.get(i).getNumOfLoans() + 1);
successful = true;
}
}
public void loanBook(){
扫描仪输入=新扫描仪(System.in);
布尔成功=假;
做{
System.out.println(
“\n请输入要取出的图书的图书ID(按9键退出主菜单)”;
if(input.nextInt()==9){
成功=正确;
打破
}
int bookID=input.nextInt();
对于(int i=0;i
if (input.nextInt() == 9) {
你读取了数字,如果不是9,你可以继续。但你也放弃了它。这就是为什么你必须再次输入它。一个简单的解决方案是使用一个变量,这是你已经做过的
int bookID = input.nextInt();
if (bookID == 9) // check the save value so you don't have to enter it twice.
break;
如果你想知道,我怎么能自己解决这个问题呢?答案是使用你的调试器,因为这应该正好是每行代码正在做的事情