java 二维数组clone_java - 如何添加java.util.Scanner类以读取输入项的二维数组? - 堆栈内存溢出...

我有一个关于使用java.util.Scanner类读取浮点输入的二维数组的问题,我试图在Internet上找到它,但是仍然无法正常工作。 该代码向我显示了错误。 任何人都可以为我检查我错了哪一部分? 下面是我的代码:

import java.util.Scanner;

public class CloneTwoDarray {

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[][];

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\":");

String append = "";

while (sc.hasNextLine()) {

String input = sc.nextLine();

if ("-1".equals(input)) {

break;

}

lines.add(input);

}

sc.close();

System.out.println("\n\nThe result is:\n");

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!!!");

}

}

}

输出错误显示如下:

C:\Users\User\AppData\Local\NetBeans\Cache\8.0.2\executor-snippets\run.xml:48:

Cancelled by user.

BUILD FAILED (total time: 3 seconds)

实际上,我希望输出如下所示:

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)

希望有人可以帮助我解决这个问题。 谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值