编程新手,你们能告诉我在java中进行多行输入的最佳方法吗?有点像这样。
程序首先询问用户案例的数量。 然后要求用户输入 2 个整数,并用空格分隔。
第一列仅表示列数。 id 也希望能够获得第二列整数的总和(25000+1000=?)
sample input
2
1 25000
2 1000
sample output
26000
最佳答案
试试这个
import java.util.Scanner;
public class Launcher {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int inputs = scanner.nextInt();
int sum = 0;
while (inputs-- > 0) {
// input 1 2500 in one line is read as two different inputs
int row = scanner.nextInt();
int value = scanner.nextInt();
sum += value;
}
System.out.println(sum);
}
}
}
你可以尝试一下
sample input
2
1 25000
2 1000
sample output
26000
关于JAVA:多行输入并用空格分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53417454/