gpt4 book ai didi

java - java中跳过输入行

转载 作者:行者123 更新时间:2023-12-02 07:16:38 25 4
gpt4 key购买 nike

因此,我有一个 addItem() 方法,可以在其中将书籍、磁带或 cd 对象添加到我的库存中。为了存储书籍、磁带或 CD 对象,我从用户处获取对象的标题、作者和价格。但是,当我运行该程序时,它会跳过“请输入标题:”并直接转到“请输入作者:”。然后,当我让它显示信息时,标题字段为“null”。

static void addItem(int type) {     

String title;
String author;
Double price;

System.out.println("Please enter the title");
title = keyboard.nextLine()

System.out.println("Please enter the author");
author = keyboard.nextLine();

System.out.println("Please enter the price");
price = keyboard.nextDouble();


if(type == 0){
Book BookStoreItem;

BookStoreItem = new Book(title, author, price);

System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}

if(type == 1){
Tape BookStoreItem;

BookStoreItem = new Tape(title, author, price);

System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}

if(type == 2){
CD BookStoreItem;

BookStoreItem = new CD(title, author, price);

System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}


}//end of addItem()

这是一个示例输出:

Press:
0 To add a book to the inventory
1 To add a tape to the inventory
2 To add a Cd to the inventory
Please enter your choice: 0
Please enter the title:
Please enter the author: John Smith
Please enter the price: 55.50

Title: null
Author: John Smith
Price: 55.5

这是带有菜单的更新:

static void printMenu() {
System.out.println("\nPress:");
System.out.println("\t" + ADD_BOOK + "\tTo add a book to the inventory\n");
System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the inventory\n");
System.out.println("\t" + ADD_CD + "\tTo add a CD to the inventory\n");
System.out.println("\t" + SHOW_ITEM_LIST + "\tTo display a list of available books\n");
System.out.println("\t" + SEARCH_ITEM + "\tTo search for a specific book\n");
System.out.println("\t" + QUIT + "\tTo exit\n");
}

以及选择:

static void runBookStoreApplication() {
int userChoice = QUIT;
String quitMessage = "\nThank you for using the BookStoreApplication!";
String invalidOptionMessage = "\nInvalid option!";

do{
printMenu();
userChoice = getUserChoice();

switch (userChoice) {
case ADD_BOOK:
addItem(0);
break;
case ADD_TAPE:
addItem(1);
case ADD_CD:
addItem(2);
case SHOW_ITEM_LIST:
showItemList();
break;
case SEARCH_ITEM:
searchItem();
break;
case QUIT:
System.out.println(quitMessage);
break;
default:
System.out.println(invalidOptionMessage);
}
} while(userChoice != QUIT);

}//end of runBookStoreApplication

使用 getUserChoice:

static int getUserChoice() {
int choice;

System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();

return choice;
}

最佳答案

您没有向我们展示您询问“请输入您的选择:”的代码,但我敢打赌您正在使用 keyboard.nextInt() 或其他东西。此方法不会消耗所有行,仅消耗整数 0,因此当您随后调用 nextLine() 时,它实际上会立即返回该行的其余部分(即,带有空字符串)。

要解决此问题,请在方法之前调用 keyboard.nextLine()

编辑:

现在您已经放置了 getUserChoice() 方法,您可以看到有一个对 keyboard.nextInt() 的调用,正如我推测的那样。只需在其后调用 nextLine() 即可消耗所有行,并且扫描仪已准备好进行下一个输入:

static int getUserChoice() {
int choice;

System.out.print("Please enter your choice: ");
choice = keyboard.nextInt(); // consume just the integer

// consume the rest of the line
keyboard.nextLine();

return choice;
}

关于java - java中跳过输入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com