【发布时间】:2019-10-24 18:47:44
【问题描述】:
我有一个关于使用 java.util.Scanner 类读取输入的问题。
下面是我的编码,没有使用 java.util.Scanner 类来读取输入:
public class NewTry {
public static float[][] clone(float[][] a) throws Exception {
float b[][] = new float[a.length][a[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
b[i][j] = a[i][j];
}
}
return b;
}
public static void main(String args[]) {
float[][] a = new float[][] { { 1.513f, 2.321f, 3.421f }, { 4.213f, 5.432f, 6.123f },
{ 7.214f, 8.213f, 9.213f } };
try {
float b[][] = clone(a);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}
不使用 java.util.Scanner 显示输出:
run:
1.513 2.321 3.421
4.213 5.432 6.123
7.214 8.213 9.213
BUILD SUCCESSFUL (total time: 3 seconds)
我的问题是如何在编码中添加 java.util.Scanner 类来读取没有默认浮点数的输入?是否使用数组运行扫描仪?
其实我想要的样例输出如下(浮点数必须自己输入):
run:
Type nine float numbers two-dimensional array of similar type and size with line breaks, end
by"-1":
1.513
2.321
3.421
4.213
5.432
6.123
7.214
8.213
9.213
-1
The result is:
1.513 2.321 3.421
4.213 5.432 6.123
7.214 8.213 9.213
BUILD SUCCESSFUL (total time: 11 second)
希望有人可以指导我或修改我的编码以添加 java.util.Scanner 类来读取输入。谢谢。
【问题讨论】:
-
@Abra 好的。您可以修改我的代码以添加 Scanner 类吗?谢谢。