我知道這個網站對像我這樣的新手來說可能相當苛刻,但我已經嘗試了三天才得到這個權利。這是最終項目的作業分配的一部分。我遇到的問題是當我多次使用案例1時,第一次按計劃運行,但第二次似乎忽略了以下幾行。全球掃描儀工作一次,但在第二次運行時會有點毛茸茸
System.out.println("Please enter the title: ");
String title = two.nextLine();
,我注重的區域是在add()方法,並在主類的書店類,案例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:
你爲什麼要創建如此多的'Scanner'實例? –
除了@ElliottFrisch的問題,你還可以在你的main方法中調用'in.close()',這會關閉stdin,這將使stdin上的其他掃描器無法工作。在stdin上合併爲單個掃描器,並且不要關閉它。 – KevinO
@ElliottFrisch,我瞭解您對多個Scanner實例的關注。我發佈的所有代碼都非常難看,因爲熟悉java的任何人的標準和我正在努力改進。我非常感謝你們在這裏給予的幫助,我現在已經回到Eclipse來實施你們所提到的修復和建議。 – PoundingHead