如何使用java.util.Scanner类读取输入?
我有一个关于使用java.util.Scanner类读取输入的问题 下面是我在不使用java.util.Scanner类读取输入的情况下编写的代码:如何使用java.util.Scanner类读取输入?,java,arrays,Java,Arrays,我有一个关于使用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]; fo
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:
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类来读取输入。谢谢。Scanner scan=new Scanner(System.in);
Scanner scan =new Scanner(System.in);
float[][] a = new float[3][3];
for(int i =0 ;i<3;i++) {
for(int j=0; j<3; i++) {
a[i][j] =scan.Float();
}
}
浮动[][]a=新浮动[3][3];
对于(inti=0;i检查此项
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);
System.out.println ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
float[][] a = new float[3][3];
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
break;
}
a[i][j]=Float.parseFloat(line);
}
}
System.out.println("\n The result is:");
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!!!");
}
}
@Abra好的。你能修改我的代码来添加扫描器类吗?谢谢。你能在我的编码中添加你的答案吗?谢谢。谢谢你的答案-虽然代码/配置片段可能会提供一些有限的短期帮助,但通过说明为什么这是一个很好的问题解决方案来正确解释它的长期价值,并使它对未来的rea更有用还有其他类似的问题。请编辑您的答案以添加一些解释,包括您所做的假设。@loannis Barakos如何停止数组中的限制?如果您想无限地插入数字直到输入-1,最简单的方法是使用最大数字设置2d数组长度,例如[1000][3]。然后for循环的i应该增加到1000(或.length)。当输入-1时,您可以将i存储到一个变量中,这样您就知道数组已填充到i。另一个解决方案是创建一个2d Arraylist。示例
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);
System.out.println ("Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:");
float[][] a = new float[3][3];
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
String line = sc.nextLine();
if ("-1".equals(line)){
break;
}
a[i][j]=Float.parseFloat(line);
}
}
System.out.println("\n The result is:");
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!!!");
}
}
Type nine float numbers two-dimensional array of similar type and size with line breaks, end by -1:
1.1
1.2
1.3
2.1
2.2
2.3
3.1
3.2
3.3
The result is:
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3