Java 从用户处获取输入总是少于1

Java 从用户处获取输入总是少于1,java,java.util.scanner,Java,Java.util.scanner,我试图从用户那里获取n行输入(字符串) 首先设置n 我初始化字符串数组以保存输入,并初始化for循环以保存它们 问题是它总是没有意义 如果n=1,程序终止 如果n=2,则只需要1个输入 如果n=3,则只需2个输入,以此类推 怎么了 Scanner sc = new Scanner(System.in); //how many lines should be taken int lines = sc.nextInt(); // initialize input array String[

我试图从用户那里获取n行输入(字符串)

  • 首先设置n
  • 我初始化字符串数组以保存输入,并初始化for循环以保存它们
问题是它总是没有意义

  • 如果n=1,程序终止
  • 如果n=2,则只需要1个输入
  • 如果n=3,则只需2个输入,以此类推
怎么了

Scanner sc = new Scanner(System.in);
//how many lines should be taken
int lines = sc.nextInt(); 
// initialize input array
String[] longWords = new String[lines] ;

//set the input from the user into the array
for (int i = 0; i < longWords.length; i++) {
    longWords[i] = sc.nextLine() ;
}
Scanner sc=新扫描仪(System.in);
//应该排多少行
int lines=sc.nextInt();
//初始化输入数组
String[]longWords=新字符串[行];
//将用户的输入设置到数组中
for(int i=0;i
这样做:

Scanner sc = new Scanner(System.in);
//how many lines should be taken
int lines = sc.nextInt(); 

//read the carret! This is, the line break entered by user when presses Enter
sc.nextLine();

// initialize input array
String[] longWords = new String[lines] ;

//set the input from the user into the array
for (int i = 0; i < longWords.length; i++) {
    longWords[i] = sc.nextLine() ;
}
Scanner sc=新扫描仪(System.in);
//应该排多少行
int lines=sc.nextInt();
//读卡雷特!这是用户在按Enter键时输入的换行符
sc.nextLine();
//初始化输入数组
String[]longWords=新字符串[行];
//将用户的输入设置到数组中
for(int i=0;i
这样做:

Scanner sc = new Scanner(System.in);
//how many lines should be taken
int lines = sc.nextInt(); 

//read the carret! This is, the line break entered by user when presses Enter
sc.nextLine();

// initialize input array
String[] longWords = new String[lines] ;

//set the input from the user into the array
for (int i = 0; i < longWords.length; i++) {
    longWords[i] = sc.nextLine() ;
}
Scanner sc=新扫描仪(System.in);
//应该排多少行
int lines=sc.nextInt();
//读卡雷特!这是用户在按Enter键时输入的换行符
sc.nextLine();
//初始化输入数组
String[]longWords=新字符串[行];
//将用户的输入设置到数组中
for(int i=0;i
这样做:

Scanner sc = new Scanner(System.in);
//how many lines should be taken
int lines = sc.nextInt(); 

//read the carret! This is, the line break entered by user when presses Enter
sc.nextLine();

// initialize input array
String[] longWords = new String[lines] ;

//set the input from the user into the array
for (int i = 0; i < longWords.length; i++) {
    longWords[i] = sc.nextLine() ;
}
Scanner sc=新扫描仪(System.in);
//应该排多少行
int lines=sc.nextInt();
//读卡雷特!这是用户在按Enter键时输入的换行符
sc.nextLine();
//初始化输入数组
String[]longWords=新字符串[行];
//将用户的输入设置到数组中
for(int i=0;i
这样做:

Scanner sc = new Scanner(System.in);
//how many lines should be taken
int lines = sc.nextInt(); 

//read the carret! This is, the line break entered by user when presses Enter
sc.nextLine();

// initialize input array
String[] longWords = new String[lines] ;

//set the input from the user into the array
for (int i = 0; i < longWords.length; i++) {
    longWords[i] = sc.nextLine() ;
}
Scanner sc=新扫描仪(System.in);
//应该排多少行
int lines=sc.nextInt();
//读卡雷特!这是用户在按Enter键时输入的换行符
sc.nextLine();
//初始化输入数组
String[]longWords=新字符串[行];
//将用户的输入设置到数组中
for(int i=0;i
问题在于
nextInt()
不处理输入行末尾的换行符,因此当您输入1时,
Scanner
会看到'1',\n',但只接受'1'。然后,当您调用
nextLine()
时,它会看到剩余的换行符,然后立即返回。这是一个很难找到的错误,但请记住换行符是否仍在等待处理。

问题在于
nextInt()
不处理输入行末尾的换行符,因此当您输入1时,例如,
Scanner
会看到'1',\n',但只接收'1'。然后,当您调用
nextLine()
时,它会看到剩余的换行符,然后立即返回。这是一个很难找到的错误,但请记住换行符是否仍在等待处理。

问题在于
nextInt()
不处理输入行末尾的换行符,因此当您输入1时,例如,
Scanner
会看到'1',\n',但只接收'1'。然后,当您调用
nextLine()
时,它会看到剩余的换行符,然后立即返回。这是一个很难找到的错误,但请记住换行符是否仍在等待处理。

问题在于
nextInt()
不处理输入行末尾的换行符,因此当您输入1时,例如,
Scanner
会看到'1',\n',但只接收'1'。然后,当您调用
nextLine()
时,它会看到剩余的换行符,然后立即返回。这是一个很难找到的错误,但请记住换行符是否仍在等待处理