gpt4 book ai didi

java - 如何在java中为多行输入n个空格?

转载 作者:行者123 更新时间:2023-12-01 16:25:46 25 4
gpt4 key购买 nike

11 22 33

44 54 63

73 53 24

我无法在 java 中接受这种格式的输入。我对单行的 n 间隔输入感到满意,但无法采用竞争性编码中使用的 n 间隔多行输入。任何人都可以为我提供一个简单的解决方案。

最佳答案

您可以使用扫描仪来实现此目的:

import java.util.Scanner;
public class GetInput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] numbers = new int[3][3];

for(int i = 0; i < 3; i++) { //Iterating over the lines - in this case the user has to enter 3 lines
for(int j = 0; j < 3; j++) { //User has to enter 3 numbers per line
numbers[i][j] = scan.nextInt(); //Scans the next int. It is not a problem that you don't press enter after each number
}
}

//Output - just to test if all went correct
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println("");
}

}
}

关于java - 如何在java中为多行输入n个空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62152127/

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