Java为什么只读取输入的第二行?

问题描述 投票:0回答:1
  1. 首先,正如您所知,我是编码和这个网站的新手。我的代码存在以下问题:我在控制台的第一行中输入了我的输入,但是直到我输入任何内容并将其输入到第2行中时,它才读取。例如:我输入“ Jayden”作为名称,按Enter键,什么也没有发生。然后输入(在此处插入任何字母),然后打印“您的名称超过3个字母,并以字母J!开头”。有什么想法吗?

import java.util。*;公共类随机{

public static void main(String[] args) {
    // TODO Auto-generated method stub



try (Scanner number = new Scanner(System.in)) {
    if (number.nextLine().startsWith("J") && number.nextLine().length() > 3 )
        {


        System.out.println("Your name is over 3 letters and starts with the letter J!");

    }

    else if  (number.nextLine().length() < 3)
    {
        System.out.println("Your name is too short!");

    }

    else if (number.nextLine().length() > 10)
    {
        System.out.println("Your name is too long!");


    }
}








}
}
java
1个回答
1
投票

只需将其添加到try块的开始(声明变量)之后:

String numberString = number.nextLine()

然后在您的条件下用变量替换number.nextLine()

© www.soinside.com 2019 - 2024. All rights reserved.