简体   繁体   English

似乎无法弄清楚如何忽略循环中最左侧和最右侧的数组元素

[英]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.

相关问题 似乎无法弄清楚为什么我的循环不会进展 - Can't seem to figure out why my loop wont progress 似乎无法弄清楚如何在Android中启动服务 - Can't seem to figure out how to start a service in Android 似乎无法弄清楚如何使用primefaces重定向到.xhtml - Can't seem to figure out how to redirect to a .xhtml using primefaces 2D阵列从左到右,然后从右到左输出。 似乎无法获得正确的元素 - 2D Array outputting from left to right, then right to left. Can't seem to get the correct element 无法弄清楚For Loop - Can't figure out For Loop (Java)我无法弄清楚如何使用所需的计数器控制循环来填充数组 - (Java) I can't figure out how to populate an array with a counter controlled loop which is required 在第 16 行从二维数组输入中获取 InputMismatchException 并且我似乎无法弄清楚为什么 - Getting An InputMismatchException On Line 16 From A 2D Array Input And I Can't Seem To Figure Out Why ParseException-无法找出正确的模式 - ParseException - Can't figure out the right pattern 我似乎无法弄清楚如何打印所有包括重复的单词 - I can't seem to figure out how to get this print out all the words including the duplicates Android Java:无法弄清楚如何循环我的VideoView - Android Java: Can't figure out how to loop my VideoView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM