程序(java)跳过nextLine()提示符并继续执行程序

时间:2015-04-28 06:25:33

标签: java

这是代码:

import java.util.Scanner;

public class DriverProject1
{
public static void main(String[] args)
{

    int roomNumber;
    int numberOfRooms;
    int optionNumber;
    String guestName = null;
    String phoneNumber;
    int nightsStaying;
    double nightlyRate;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many rooms are in the hotel?");
    numberOfRooms = keyboard.nextInt();
    Hotel Carnegie = new Hotel(numberOfRooms);
    do
    {
        System.out.println("Welcome to the MENU!");
        System.out.println("Please enter a number corresponding to the option desired."); // main menu screen
        System.out.println("OPTION 1: Put guest into room");
        System.out.println("OPTION 2: Remove guest from room");
        System.out.println("OPTION 3: Get guest info");
        System.out.println("OPTION 4: Get guest count");
        System.out.println("OPTION 5: Display guest book");
        System.out.println("OPTION 6: Calculate expected income");
        System.out.println("OPTION 7: Quit");

        optionNumber = keyboard.nextInt(); // choosing an option
        if (optionNumber == 1) // if option 1 is chosen
        {

            System.out.println("What is the guests first name?");
            guestName = keyboard.nextLine();
            System.out.println("What is the guests Phone number?");
            phoneNumber = keyboard.nextLine();
            Guest newGuest = new Guest(guestName);
            newGuest.setPhoneNumber(phoneNumber);

"客人的名字是什么?什么是客人电话号码?"出现,它不会取任何客人名称。 guestName已初始化为""。第36-39行。不确定这个问题是怎么回事。我正在使用我的酒店类中包含的一些方法,但我几乎可以肯定它们不包含导致此错误的错误。 do循环也将在稍后的代码中完成。

2 个答案:

答案 0 :(得分:0)

{{1}}

通过添加keyboard.nextLine()跳过一行;在读客人之前。这对我有用。

答案 1 :(得分:-2)

如下所示更改代码然后它将起作用

import java.util.Scanner;

public class DriverProject1
{
public static void main(String[] args)
{

    int roomNumber;
    int numberOfRooms;
    int optionNumber;
    String guestName = null;
    String phoneNumber;
    int nightsStaying;
    double nightlyRate;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many rooms are in the hotel?");
    numberOfRooms = Integer.parseInt(keyboard.nextLine());
    Hotel Carnegie = new Hotel(numberOfRooms);
    do
    {
        System.out.println("Welcome to the MENU!");
        System.out.println("Please enter a number corresponding to the option desired."); // main menu screen
        System.out.println("OPTION 1: Put guest into room");
        System.out.println("OPTION 2: Remove guest from room");
        System.out.println("OPTION 3: Get guest info");
        System.out.println("OPTION 4: Get guest count");
        System.out.println("OPTION 5: Display guest book");
        System.out.println("OPTION 6: Calculate expected income");
        System.out.println("OPTION 7: Quit");

        optionNumber = Integer.parseInt(keyboard.nextLine());; // choosing an option
        if (optionNumber == 1) // if option 1 is chosen
        {

            System.out.println("What is the guests first name?");
            guestName = keyboard.nextLine();
            System.out.println("What is the guests Phone number?");
            phoneNumber = keyboard.nextLine();
            Guest newGuest = new Guest(guestName);
            newGuest.setPhoneNumber(phoneNumber);