Java 中连续输入二维数组的方法有:直接从键盘输入,使用嵌套循环。使用 Scanner 类从 System.in 读取输入。使用BufferedReader 类从 System.in 读取输入。

如何在 Java 中连续输入二维数组
直接输入法
使用嵌套循环直接从键盘输入每个数组元素,如下所示:
int[][] array = new int[numRows][numCols];for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { System.out.print("Enter element [" + i + "][" + j + "]: "); array[i][j] = Integer.parseInt(System.console().readLine()); }}
Scanner 类
立即学习“Java免费学习笔记(深入)”;
阿里云-虚拟数字人
阿里云-虚拟数字人是什么? …
2 查看详情
使用 Scanner 类从 System.in 读取输入,如下所示:
Scanner scanner = new Scanner(System.in);int[][] array = new int[numRows][numCols];for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { System.out.print("Enter element [" + i + "][" + j + "]: "); array[i][j] = scanner.nextInt(); }}scanner.close();
BufferedReader 类
使用 BufferedReader 类从 System.in 读取输入,如下所示:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int[][] array = new int[numRows][numCols];for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { System.out.print("Enter element [" + i + "][" + j + "]: "); array[i][j] = Integer.parseInt(reader.readLine()); }}reader.close();
以上就是java怎么连续输入二维数组的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/399883.html
微信扫一扫
支付宝扫一扫