简体   繁体   English

如何让这行未执行的代码执行?

[英]How do I get this unexecuted line of code to execute?

I have an "Appointment" class as follows我有一个“约会”课程如下

private String month;
private int day;
private int hour, minute;
private String message;

Appointment() {
    month = "Jan";
    day = 1;
    hour = 12;
    minute = 0;
    message = "message";
}

private void setMonth(String monthIn) {
    if (monthIn.length() > 3) {
        System.out.println("Re-enter and keep month to 3 letters");
    } else {
        month = monthIn;
    }
}

private void setDay(int dayIn) {
    day = dayIn;
}

private void setHour(int hourIn) {
    hour = hourIn;
}

private void setMinute(int minuteIn) {
    minute = minuteIn;
}

private void setMessage(String messageIn) {
    message = messageIn;
}

public void inputAppointment() {
    System.out.println("Input appointment (month, day, hour, minute, message)");
    setMonth(UserInput.getString());
    setDay(UserInput.getInt(1, 31));
    setHour(UserInput.getInt(1, 12));
    setMinute(UserInput.getInt(0, 59));
    setMessage(UserInput.getString());
}

In my main method i have在我的主要方法中,我有

Appointment app = new Appointment();
app.inputAppointment();

But I'm only able to input the first string and the following 3 integers.但我只能输入第一个字符串和以下 3 个整数。 setMessage(UserInput.getString()); setMessage(UserInput.getString()); never executes.从不执行。 The arguments sent to the set methods in inputAppointment() call methods from a "UserInput" class that handles input as follows发送到 inputAppointment() 中的 set 方法的参数从处理输入的“UserInput”类调用方法,如下所示

public static String getString() {
    return scan.nextLine();
}

public static int getInt() {
    return scan.nextInt();
}

public static int getInt(int min, int max) {
    int input = getInt();
    if (input >= min && input <= max) {
        return input;
    } else {
        System.out.println("Invalid input. Re-enter...");
        return getInt(min, max);
    }
}

Every time the user inputs text into the console and presses enter, the user's input plus the end of line characters '\\r\\n' are added to the input buffer.每次用户在控制台输入文本并按下回车键时,用户的输入加上行尾字符 '\\r\\n' 被添加到输入缓冲区。

scan.nextLine() reads to the next '\\r\\n' characters and returns what it found. scan.nextLine()读取下一个 '\\r\\n' 字符并返回它找到的内容。

scan.nextInt() will read ahead until it has scanned in the next int, but it will not consume the '\\r\\n' from the user pressing enter. scan.nextInt()将提前读取,直到它在下一个 int 中扫描,但它不会消耗用户按下 Enter 的 '\\r\\n'。 After your 3rd call there is still a '\\r\\n' on the input buffer and your next call to scan.nextLine() assumes this is the user's next line of input.在您第三次调用之后,输入缓冲区上仍然有一个 '\\r\\n' 并且您对scan.nextLine()下一次调用假定这是用户的下一行输入。

If you are expecting the user to enter an integer on separate lines, then you need an extra call to scan.nextLine() to consume the end of line characters.如果您希望用户在单独的行上输入一个整数,那么您需要额外调用scan.nextLine()以使用行尾字符。

ie IE

public static int getInt() {
    int input = scan.nextInt();
    scan.nextLine();
    return input;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM