[英]Can't seem to figure out how to ignore left and right most array elements in loop
I'm trying to fill an array made by the user (between size 3-11 odd) with characters in certain element position to get patterns.我试图用特定元素位置的字符填充用户创建的数组(大小介于 3-11 奇数之间)以获取模式。 What the user inputs acts as both the number of rows and columns so If they put in a 5 like in my example below, they'd get a 5 by 5 array.用户输入的内容既作为行数也作为列数,所以如果他们像下面的例子一样输入 5,他们会得到一个 5 x 5 的数组。 I'm trying to get this pattern我正在尝试获得这种模式
-----------
* * * * *
* * *
*
-----------
-----------
* * * * *
* * * * *
* * * * *
-----------
Here's the code这是代码
public static void main (String [] args) {
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = pointDown(star,dimension);
System.out.println();
print(array2d);
}
public static void print(char [] [] arrayParam) {
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
System.out.println();
for(char[] row : arrayParam)
{
for(char c : row)
System.out.print(" " + c);
System.out.printf("\n");
}
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
}
The problem should be in this method, the loop after this one I think问题应该出在这个方法上,我认为这个循环之后
public static char [] [] pointDown (char starParam, int dimenParam) {
char [] [] pointDown = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
pointDown[i][j] = ' ';
// I fill the positions first with blank spaces then add the characters
// with the loop below
}
}
/* Problem should be in this loop, Is there even a pattern to it though
* since columns would have to account for both the last and beginning
* columns after the first loop? Should I make variables for those or is
*/ there a simpler way to do it that I'm missing?
for (int i = 0; i <= dimenParam/2; i++) {
for (int j = 0; j < dimenParam; j++) {
pointDown[i][j] = starParam;
}
}
return pointDown;
}
Update: After taking into account what I've been told I was able to figure out the issue.更新:在考虑到我被告知的内容后,我能够找出问题所在。 Here's what the code should look like下面是代码的样子
char [] [] pointDown = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
pointDown[i][j] = ' ';
// As before this part fills the array with blank spaces
}
}
int columnEnd = dimenParam; // Set up a variable to hold how far the column goes to
int j = 0;
for (int row = 0; row <= dimenParam/2; row++) {
for (int column = j; column < columnEnd; column++) {
pointDown[row][column] = starParam;
}
columnEnd--; // I had to decrease the ending column in the outer loop
j++; // Originally I had this in the inner loop for the longest time
// By moving it to the outer loop I avoid out of Bounds and runtime errors
}
return pointDown;
I'm pretty sure I still over complicated things but I'm happy with this code我很确定我仍然在复杂的事情上,但我对这段代码很满意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.