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

程序(java)跳过nextLine()提示符并继续执行程序,java,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;

代码如下:

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行。我不确定这个问题是否还在继续。我正在使用我的hotel类中包含的一些方法,但是我几乎可以肯定它们不包含导致此错误的错误。此外,do循环将在代码的后面部分完成

如下图所示更改代码,它将正常工作

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);

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

->你能给我们提供更多的信息吗?我不明白你的问题和
键盘是什么
?你在你的程序中使用的是
nextLine()
以外的东西吗?例如
nextInt()
nextDouble()
等等?guestName正在取值,但是它没有被分配给newGuest的名称,因为它发生在输入被删除之后taken@Will发布你在这里引用的完整代码。行为没有改变
keyboard.nextLine();
guestName = keyboard.nextLine();