gpt4 book ai didi

java - 全局扫描仪可以工作一次,但在第二次运行时会变得有点毛茸茸的

转载 作者:行者123 更新时间:2023-11-30 07:25:43 25 4
gpt4 key购买 nike

我知道这个网站对于像我这样的菜鸟来说可能相当苛刻,但我已经尝试了三天来解决这个问题。这是期末项目作业的一部分。我遇到的问题是,当我多次使用案例 1 时,第一次它按计划工作,但第二次它似乎跳过了以下几行。

System.out.println("Please enter the title: ");
String title = two.nextLine();

我关注的领域是 add() 方法中的 Bookstore 类和主类中的情况 1。当尝试添加第二本书时,代码立即跳过书名并直接转到国际标准书号 (ISBN)。

import java.util.Scanner;
public class MIS_Tester {
public static Scanner two = new Scanner(System.in);
public static Scanner three = new Scanner(System.in);
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Bookstore bookseller = new Bookstore();
Order addBook = new Order();
Sale sell = new Sale();
boolean finished = false;
while(!finished){
System.out.println("Select: (0) output; (1) add book; (2) delete book; (3) order book;"
+ "(4) sell a book; (5) exit");
int option = in.nextInt();
switch(option){
case 0: bookseller.ouput();break;
case 1: bookseller.add(two);break;
case 2: bookseller.delete(two);break;
case 3: addBook.add(three);break;
case 4: sell.sellBook(two);break;
default: finished = true; break;
}
}
in.close();
}
}

class Bookstore{
private Book[] catalog;
private int numBooks;
private final int MAXBOOKNUM = 100;
Scanner nine = new Scanner(System.in);

public Bookstore(){
numBooks = 0;
catalog = new Book[MAXBOOKNUM];
}
public void ouput(){
for (int i = 0; i < numBooks; i++) catalog[i].output();
}
public void add(Scanner two){
System.out.println("Please enter the title: ");
String title = two.nextLine();
System.out.println("Enter the ISBN: ");
int isbn = two.nextInt();
System.out.println("Enter the publication date: ");
int month = two.nextInt();int day = two.nextInt();int year = two.nextInt();
catalog[numBooks++] = new Book(title, isbn, new Date(month, day, year));
}
public void delete(Scanner two){
System.out.println("enter the one to delete");
int option = two.nextInt();
int numOfElements = catalog.length - (option +1);
System.arraycopy(catalog, option + 1, catalog, option, numOfElements);
}
}

class Book{
private String title;
private Date pubDate;
private int isbn;
private int orderNumber;

public Book(String title, int isbn, Date pubDate){
this.title = title;
this.isbn = isbn;
this.pubDate = pubDate;
}

public Book(String title, int isbn, Date pubDate, int orderNumber){
this.title = title;
this.isbn = isbn;
this.pubDate = pubDate;
this.orderNumber = orderNumber;
}
public void output(){
System.out.println("Title: " + "\"" +title + "\"");
System.out.println("ISBN: " + isbn);
pubDate.output();
}
public void orderOut(){
System.out.println("The book " + "\"" + title + "\"" + " has been ordered.");
}
}

控制台日志:

Select: (0) output; (1) add book; (2) delete book; (3) order book;(4) sell a book; (5) exit
1
Please enter the title:
All About Dogs
Enter the ISBN:
1234
Enter the publication date:
12
29
1978
Select: (0) output; (1) add book; (2) delete book; (3) order book;(4) sell a book; (5) exit
1
Please enter the title:
Enter the ISBN:

最佳答案

所以在java api中位于 here.

nextLine() - Advances this scanner past the current line and returns the input that was skipped.

因此,此方法将扫描仪前进到新行并返回跳过的输入。

nextInt() - Scans the next token of the input as an int.

此方法仅获取输入的下一个标记作为 int。它不消耗换行符。

所以基本上发生的事情是:

当您使用 System.out.println 时,您正在创建换行符但 nextInt() 方法不消耗换行符,因此它们保留在输入缓冲区中。因此,当您第二次调用 nextLine() 时,它仅返回输入缓冲区中下一个的换行符(剩余的)。我希望这是有道理的,如果我应该详细说明的话,请告诉我。

解决方法:- 添加two.nextLine() //after each call to two.nextInt()

因此,更新后的 add() 方法将如下所示:

public void add(Scanner two){
System.out.println("Please enter the title: ");
String title = two.nextLine();
System.out.println("Enter the ISBN: ");
int isbn = two.nextInt();
two.nextLine(); // consume the left over newline token in the buffer
System.out.println("Enter the publication date: ");
int month = two.nextInt();int day = two.nextInt();int year = two.nextInt();
two.nextLine(); // consume the left over newline token in the buffer
catalog[numBooks++] = new Book(title, isbn, new Date(month, day, year));
}

请注意,此代码未经测试。

关于java - 全局扫描仪可以工作一次,但在第二次运行时会变得有点毛茸茸的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831153/

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