nextLine()在for循环后不起作用 [英] nextLine() not working after a for loop

查看:111
本文介绍了nextLine()在for循环后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么我的 nextLine()在以下代码中的for循环之后无效?它总是跳过 nextLine()并直接转到 if 语句。

Does any one know why my nextLine() is not working after a for loop in the following code? It's always skipping the nextLine() and going direct to the if statement.

int[] a = new int[3]

for(int i=0; i<3;i++)
{
  a[i] = nextInt();
}

String b = nextLine();

if(b == "go")
{
  .......
}
else
{
  ..... 
}


推荐答案

我猜你可能正在做的是要求用户输入整数,然后按回车键。当它解析整数时,它只解析并删除整数。但当然,他们所推出的新线仍然在流。然后当你要求 nextLine()时,它会立即返回,因为流中已经有一行(在整行中按下整行的行) 。也许你实际上期望 nextLine()在输入整数之后等待额外的一行,但这不是正在发生的事情。这是对吗?

I am guessing that maybe what you are doing is asking for the user to input the integers and then press enter. When it parses the integers, it just parses and removes the integers. But of course the newline that they pressed is still in the stream. And then when you ask for nextLine(), it returns immediately because there is already a line in the stream (the one that ends in the newline that was pressed for the integers). And maybe you had actually expected the nextLine() to wait for an additional line after the integers had been entered, but that is not what is happening. Is this right?

这篇关于nextLine()在for循环后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆