java一行输入多个数据_java – 在一行中获取多个输入,直到用户按Enter键

我想要做的是:

2 (number of test cases)

4 7 8 15 16 (test case 1)

here will be output, for example will print the max number

7 97 1 2 9 (test case 2)

here is output again

我会在每个测试用例中都有一些语句,直到用户按Enter键.我看到了一些类似的问题,但我尝试过的解决方案都没有用.

这是我最后尝试过的:

Scanner cin = new Scanner(System.in);

int test = Integer.parseInt(cin.nextLine());

for (int k = 0; k < test; k++) {

while (cin.next() != "\\n") {

int number = cin.nextInt();

//do something

}

//print output

}

最佳答案 您可以读取整个输入行,然后创建一个新的Scanner来读取此行中的整数:

Scanner cin = new Scanner(System.in);

int test = Integer.parseInt(cin.nextLine());

for (int k = 0; k < test; k++) {

String line = cin.nextLine();

Scanner lineScan = new Scanner(line);

while (lineScan.hasNextInt()) {

// print number

System.out.println(lineScan.nextInt());

}

//print output

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值