我有一个关于中断输入的问题,因为我的代码输入两次“-1”来停止输入,实际上我想输入一次“-1”来停止输入,然后显示数组输出。
下面是我的代码:
import java.util.Scanner;
public class NewTMA {
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[]) {
Scanner sc = new Scanner (System.in);
System.out.println("enter row size");
int row = Integer.parseInt(sc.nextLine());
System.out.println("enter column size");
int column = Integer.parseInt(sc.nextLine());
System.out.println ("Type float numbers two-dimensional array of similar type and size with line break, end by -1:");
float[][] a = new float[row][column];
for (int i=0; i<row; i++) {
for (int j=0; j<column; 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!!!");
}
}
}
下面是我的输出:
run:
enter row size
3
enter column size
2
Type float numbers two-dimensional array of similar type and size with line breaks. end by -1:
1.4
2.4
-1
-1
The result is:
1.4 2.4
0.0 0.0
0.0 0.0
BUILD SUCCESSFUL (total time: 13 seconds)
实际上我只想输入一次“-1”来停止输入,但我不知道为什么输出显示两次“-1”来停止输入。希望有人能帮助我找出我做错的部分。谢谢。
哔哔one
浮云间
相关分类