0
我試圖讓用戶輸入和第二個輸入值後,我得到這個錯誤:異常線程「main」 java.util.NoSuchElementException:沒有找到行
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Reservations.start(Reservations.java:50) at Reservations.main(Reservations.java:29)
import java.util.Scanner; // Needed to read user input
public class Reservations {
// Boolean array for seating [false = available, true = taken]
static boolean[][] seats;
// Main method
public static void main(String[] args) {
// Initiates all array values to be false (available)
seats = new boolean[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
seats[i][j] = false;
}
// Welcome message
System.out.println("-------------------------");
System.out.println("Welcome to NSCC AirLines.");
System.out.println("-------------------------\n");
// Starts program
start();
}
}
public static void start() {
// Scanner needed to read users input
Scanner sc = new Scanner(System.in);
// Variables for user input
String requestedSection;
String requestedSeat;
// Counters for seating array
int countSection = 0;
int countSeat = 0;
// Prompts the user to select their choice of section
System.out.print("Please type 1 for First Class or 2 for Economy: ");
// Section preference
requestedSection = sc.nextLine();
switch (requestedSection) {
case "1":
// User selects first class
System.out.println(">>> You have selected First Class. \n");
break;
case "2":
// User selects economy
System.out.println(">>> You have selected Economy. \n");
break;
default:
// User has not selected a valid class
System.out
.println(">>> You have not selected a valid class. Please try again. \n");
start();
break;
}
// Prompts the user to select their choice of seat
System.out.print("Please type 1 for window and 2 for aisle: ");
// Seat preference
requestedSeat = sc.nextLine();
switch (requestedSeat) {
case "1":
// User selects first class
System.out.println(">>> You have selected a window seat. \n");
break;
case "2":
// User selects economy
System.out.println(">>> You have selected an aisle seat. \n");
break;
default:
// User has not selected a valid class
System.out.println(">>> You have not selected a valid seat. Please try again. \n");
start();
break;
}
// Closes Scanner
sc.close();
}
}
對不起你需要告訴我們的例外來自哪行號,你的程序正在試圖做的,你有沒有嘗試調試代碼等 – gerrytan
異常線程「main」 java.util.NoSuchElementException:否線發現 \t在java.util.Scanner.nextLine(來源不明) \t在Reservations.start(Reservations.java:50) \t在Reservations.main(Reservations.java:29) – Jonny
我想通了。我在循環的主要方法中丟失了一個方括號,它通過了。感謝所有答覆。 – Jonny