对于编程来说,你们能向我展示用java进行多行输入的最佳方法吗?像这样的小东西。
程序首先询问用户案例的数量。然后要求用户输入由空格分隔的2个整数。
第1列仅指示列计数。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
你可以使用这样的东西,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows = new Integer(scanner.nextLine());
int sum = 0;
for (int i = 0; i < rows; i++) {
sum = sum + new Integer(scanner.nextLine().split(" ")[1]);
}
System.out.println(sum);
}
}