gpt4 book ai didi

java - 如何让扫描仪读取多个数字并打印它们而无需向下一行

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:12 25 4
gpt4 key购买 nike

我是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.

关于java - 如何让扫描仪读取多个数字并打印它们而无需向下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22938031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com