我知道我必須使用Scanner類來首先創建掃描器對象來接收用戶在java中的輸入,但我不知道如何將多個單詞輸入到一個字符串變量中。例如,我有這樣的代碼在這裏:如何輸入字符串到Java?
System.out.println("Please enter the following information");
System.out.print("Author:");
author = input.nextLine();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Grade Level:");
gradeLevel = input.nextDouble();
System.out.print("Pages:");
pages = input.nextDouble();
它根據許多論壇,應該允許用戶輸入整個行成字符串變量,但相反,它會跳過作者完全提示。我究竟做錯了什麼?
編輯:我已經添加下面的完整循環:
do {
System.out.printf("What kind of book is this?\n"
+ "(0 = Textbook, 1 = Novel, 2 = Biography, 3 = Author Information, 4 = Other)");
switch(input.nextInt()) {
case 0:
System.out.println("Please enter the following information.");
System.out.print("Author:");
author = input.nextLine();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Grade Level:");
gradeLevel = input.nextDouble();
input.readLine();
System.out.print("Pages:");
pages = input.nextDouble();
//Creation of textBooks
texts[i] = new Textbook(gradeLevel, author, pages, title);
boolText = true;
break;
case 1:
System.out.print("Please enter the following information.\n");
System.out.print("Author:");
author = input.nextLine();
System.out.print("Genre:");
genre = input.next();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();
//Creation of Novels
novels[i] = new Novel(genre, title, author, pages);
boolNovel = true;
break;
case 2:
System.out.print("Please enter the following information.\n");
System.out.print("Author:");
author = input.nextLine();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Subject:");
subject = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();
//Creation of Biographies
bio[i] = new Biography(subject, title, author, pages);
boolBiography = true;
break;
case 3:
System.out.print("Please enter the following information.\n");
System.out.print("Author:");
author = input.nextLine();
System.out.print("Email:");
email = input.next();
System.out.print("Address:");
address = input.nextLine();
System.out.print("Number of Titles:");
numOfTitles = input.nextDouble();
//Creation of Author Infortmation
authors[i] = new Author(author, address, email, numOfTitles);
boolAuthor = true;
break;
case 4:
System.out.println("Please enter the following information.");
System.out.print("Title:");
title = input.nextLine();
System.out.print("Author:");
author = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();
//Creation of Author Infortmation
other[i] = new Books(title, author, pages);
boolOther = true;
default:
System.out.println("Program will now terminate.");
System.exit(1);
}
i++;
}
while (i < t);
是的,對不起,這是在執行循環內的switch語句中。 – Brian
是的!這就是訣竅!非常感謝你。 – Brian