我必须以 int 形式获取用户的出生月份并将其存储为变量以便稍后打印

I have to obtain the user's birth month in int form and store as variable for print out later

这是我的代码。

//输入

    System.out.println("Using numbers, in what month were you born? ");

    String **userMonth** = input.nextLine();

// also tried int **userMonth** = input.nextInt();

这两种方法都不行。

userMonth 不会“激活”? (抱歉菜鸟,不知道所有术语)

稍后尝试在程序错误中调用时:

    if (userMonth < 3) {
    System.out.println("Your vacation home is a van down by a river!"); 
    }

//userMonth 无法解析为变量

非常奇怪,您的变量显示为 **varName** 而不是 varName

  • 首次使用:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();

But it does not consume the newline ('ENTER').

  • 因此,紧接着,通过以下方式消耗换行符:
input.nextLine();
  • 还要确保 Scanner 声明为
Scanner input = new Scanner(System.in);
  • 不要忘记在程序结束时关闭扫描仪
input.close();