java – 在一个函数中创建一个数组,在没有return语句的情况下在另一个函数中读取
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在一个函数中创建一个数组,在没有return语句的情况下在另一个函数中读取,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2184字,纯文字阅读大概需要4分钟。
内容图文
![java – 在一个函数中创建一个数组,在没有return语句的情况下在另一个函数中读取](/upload/InfoBanner/zyjiaocheng/784/22bcebde5e78479198e0424618b2804b.jpg)
我试图在一个方法(或函数?或对象?问题 – 所有这些单词之间有什么区别?)中创建一个数组,然后在另一个方法中使用它的长度(我将在其他地方使用它作为好).我的老师告诉我,我不必返回数组,因为我只是修改了位置,所以阵列没有被破坏或什么的.我会在main中声明它然后在得到大小输入后我无法调整大小(我不认为?).
是否有人关注此事?
public class Update {
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
createMatrix();
// birthAndLive();
printMatrix();
}
public static void createMatrix() {
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
boolean[][] board = new boolean[rows][columns];
Random seedBool = new Random(seed);
}
public static void printMatrix() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
}
解决方法:
你可以通过将board传递给你的打印功能来解决这个问题.
class Update {
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
createMatrix();
// birthAndLive();
printMatrix();
}
public static void createMatrix() {
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
boolean[][] board = new boolean[rows][columns];
Random seedBool = new Random(seed);
printMatrix(board);
}
public static void printMatrix(boolean[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
}
}
我不知道你的老师允许你改变多少代码.如果需要从main调用所有函数,那么您必须将数组创建代码放在main函数中,否则您将不得不求助于return语句或类变量.
内容总结
以上是互联网集市为您收集整理的java – 在一个函数中创建一个数组,在没有return语句的情况下在另一个函数中读取全部内容,希望文章能够帮你解决java – 在一个函数中创建一个数组,在没有return语句的情况下在另一个函数中读取所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。