Когда я запускаю этот код, сканер в строке 15 ожидает моего ввода только при первом запуске цикла. В остальных случаях он не дает мне возможности ввести данные и просто возвращает пустую строку.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// implement here the program that allows the user to enter
// book information and to examine them
Scanner scanner = new Scanner(System.in);
ArrayList books = new ArrayList();
while (true) {
System.out.println("Title: ");
String title = scanner.nextLine();
if (title.equals("")) {
break;
}
System.out.println("Pages: ");
int pages = scanner.nextInt();
System.out.println("Publication year: ");
int year = scanner.nextInt();
books.add(new Book(title, pages, year));
}
System.out.println("What information will be printed?");
String choice = scanner.nextLine();
if (choice.equals("everything")) {
for (Book book : books) {
System.out.println(String.format("%s, %s, %s", book.title, book.pages, book.year));
}
} else if (choice.equals("name")) {
for (Book book : books) {
System.out.println(book.title);
}
}
}
}
этот код работает отлично.
import java.util.Scanner;
public class java {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String name = scanner.nextLine();
System.out.println(name);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... while-loop
Сканер ожидает ввода только в первый раз в цикле while [дубликат] ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Невозможно найти ошибку символа после оператора while в моем цикле do- while [дубликат]
Anonymous » » в форуме JAVA - 0 Ответы
- 24 Просмотры
-
Последнее сообщение Anonymous
-