要添加java.util.Scanner类来读取二维数组的浮点数输入,可以按照以下步骤进行:
完整代码示例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入二维数组的行数:");
int rows = scanner.nextInt();
System.out.print("请输入二维数组的列数:");
int cols = scanner.nextInt();
double[][] array = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("请输入二维数组第 " + (i + 1) + " 行,第 " + (j + 1) + " 列的浮点数:");
array[i][j] = scanner.nextDouble();
}
}
// 打印二维数组
System.out.println("输入的二维数组为:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
这样就可以使用java.util.Scanner类来读取二维数组的浮点数输入了。
领取专属 10元无门槛券
手把手带您无忧上云