Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java在for循环中使用nextLine(),_Java - Fatal编程技术网

java在for循环中使用nextLine(),

java在for循环中使用nextLine(),,java,Java,它不打印每个语句并等待输入,而是打印两个语句并等待一个输入 for(int i=0; i < size; i++) { System.out.println("course: "); courses[i] = sc.nextLine(); System.out.println("teacher: "); teachers[i] = sc.nextLine(); } for(int i=0;i

它不打印每个语句并等待输入,而是打印两个语句并等待一个输入

for(int i=0; i < size; i++)
{
    System.out.println("course: ");
    courses[i] = sc.nextLine();

    System.out.println("teacher: ");
    teachers[i] = sc.nextLine();
}
for(int i=0;i
输出:

输入课程数:2课程:

老师:


新行
已被消耗为
课程[i]=sc.nextLine()这就是它跳过的原因

解决方案:

在forLoop之前添加下一行

sc.nextLine(); //will consume the new line
for(int i=0; i < size; i++){
sc.nextLine()//将消耗新线路
对于(int i=0;i
thank you的可能重复非常有效,因此如果我理解正确,println()引起的新行就是正在使用的内容。@user3786610不,它是nextInt()。因为当您输入2时,它只会使用2,而不是它后面的新行。是的,这比ty更重要,@th3onlyn3欢迎您。:)