【发布时间】:2018-05-27 16:56:59
【问题描述】:
这篇文章会很长,但如果社区可以帮助我,我会很高兴。
这是以前课堂测试中的一道题。这次我不得不期待一个类似的,我现在被困住了。我是 Java 及其概念的新手。 测试结果应该是这样的:
我同时面临多个问题。我会尽量做到具体。首先我将解释这个任务,它给出了不能改变的条件。
任务 创建一个二维数组字段。该字段应适用于各种尺寸。除大小外,数组字段应包含以下模式(见图)。
1.) 创建一个构造函数,该构造函数根据传入的参数创建一个矩形数组字段,并带有额外的条件。
1.1) 如果参数小于5,则该字段应强制显示为5/5字段。
1.2) 如果参数大于 5,它应该始终显示为 n/n 数组。
public class Pattern {
char[][] field;
public Pattern(int n) {
// Here goes my code for creating the field
}
2.) 编写一个填充构造函数的方法。
public void fillArray() {
// Here goes my code.
}
我目前的方法
public Pattern(int n) {
field = new char[n][n];
int i, j;
if (field.length < 5) {
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
// ? what to do with the array here?
}
System.out.println();
}
}
public void fillArray() {
// no fu**ing idea...
}
public static void main (String[]args){
Pattern cross = new Pattern(2);
cross.fillArray();
System.out.println(cross);
}
问题
1.) 在构造函数中应该返回什么值?
2.) 如何访问方法中的对象并设置一个 for 循环,以获取预定义的字段大小?我应该使用this 吗?
很抱歉我对如何传递数组信息并正确执行这些信息有不好的理解。
【问题讨论】: