问题:使用扫描仪(sc)从用户那里获取输入,如果我没有使用空白sc.nextLine(),则会跳过上一行。如果我使用此空白火,则永远不会收集该变量供以后使用。
System.out.println("Please enter the Airline name: ");
String airName = sc.nextLine();
sc.nextLine(); <---------
System.out.println("Please enter the Airline code: ");
String airCode = sc.nextLine();
System.out.println("Please enter the Delta Aircraft: ");
String airCraft = sc.nextLine();
System.out.println("Please enter the first class seat capacity: ");
int firstClass = sc.nextInt();
System.out.println("Please enter the business class seat capacity: ");
int busiClass = sc.nextInt();
System.out.println("Please enter the economy class seat capacity: ");
int econClass = sc.nextInt();
System.out.println("Airline name: " + airName);
System.out.println("Airline code: " + airCode);
System.out.println("Delta Aircraft: " + airCraft);
//Splitting the first word from the rest of the string
String arr[] = airCraft.split(" ", 2);
String firstWord = arr[0];
System.out.println(firstWord + " first class seat capacity: " + firstClass);
System.out.println(firstWord + " business class seat capacity: " + busiClass);
System.out.println(firstWord + " economy class seat capacity: " + econClass);
//Airline object
A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
System.out.println(airName + " successfully added. Press Enter to continue.");
sc.nextLine();//Press Enter to continue
sc.nextLine();
//A8MainMenu.mainMenu(sc); //return to main menu after Enter.
return airline;
我的输出现在:
Airline name:
Airline code: QA
Delta Aircraft: Boeing 787
Boeing first class seat capacity: 16
Boeing business class seat capacity: 25
Boeing economy class seat capacity: 199
successfully added. Press Enter to continue.
答案 0 :(得分:0)
我最后不得不在之前放置一个空白的sc.nextLine 我开始通过扫描仪。
sc.nextLine();
System.out.println("Please enter the Airline name:");
String airName = sc.nextLine();