我是java新手,我正在尝试编写如下代码:用户逐个输入数字,代码需要立即打印每个数字,并继续接收同一行上的下一个数字
这是我到目前为止的代码:
Scanner user = new Scanner(System.in);
String value = user.nextLine();
String matrix = "";
int point = 0;
int pos = 0;
while(pos <= 5)
{
if(isInteger(value))
{
System.out.print(matrix.substring(point) + "\t");
matrix = matrix + value+",";
point+=2;
pos++;
}
else
{
System.out.print("Not a number");
}
value = user.next();
}
但每次我在程序运行时向扫描仪输入另一个数字时,它都会下降到下一行。所以在我输入 1 , 2 , 3 , 4 , 5 后,输出是:
1
2
3
4
5
我想把它做成这样:
1 2 3 4 5
有没有办法让扫描仪读取另一个数字并仍然保持在同一行?
您必须使用 nextInt()
而不是指定的 nextLine()
here.
我是一名优秀的程序员,十分优秀!