java – 我试图逐行反转一个数组

澄清一下,这是一项家庭作业.我只是在寻找建议,我不是在寻找别人为我做功课.

我上半场已经完成了.它使用两个数组来打印Asterisk设计(在这种情况下,字母’S’.工作正常.然后,我跳过两行并打印设计但翻转(所以每行反转).它似乎工作正常,但是当我运行该程序时,它打印出两个S,而第二个没有反转.任何关于我做错的想法?

public class Design {
   public static void main (String [] args) {
       char [] array = new char [150];
       for (int index = 0; index < array.length; index ++) 
       {
         array [index] = '#';
       }
       int [] indexNumbers = {
           0,1,2,3,4,5,6,7,8,9,10,20,30,40,50,
           60,70,71,72,73,74,75,76,77,78,79,89,99,109,119,129,139,140,
           141,142,143,144,145,146,147,148,149
       };
       for (int i = 0; i < indexNumbers.length; i++) 
       {
         array [indexNumbers[i]] = ' ';
       }
      for (int index = 0; index < array.length; index ++)
      {
         if (index % 10 == 0 && index > 0) 
         System.out.println();
         System.out.print (array[index]);
      }

      //Now, to reverse the letter

      System.out.println();
      System.out.println();

      int lines = 5;
      for (int i = 0; i< array.length; i++){
        if (i >= lines)
          lines += 10;
        char temp = array [i];
        array [i] = array [lines - i - 1];
        array [lines - i - 1] = temp;
      }
      for (int index = 0; index < array.length; index ++)
      {
        if (index % 10 == 0 && index > 0) 
          System.out.println();
          System.out.print (array[index]);
      }
    }
  }

编辑:是的…设计在空间,其他一切都是星号.

最佳答案 你的逆转有点困惑….如果你在两个循环中做到这一点会更容易.

for (int row = 0; row < (array.Length / 10); row++)
{
    for (int col = 0; col < 5; col++)
    {
        int rowStart = row * 10;
        int rowEnd = rowStart + 9;
        char temp = array[rowStart + col];                    
        array[rowStart + col] = array[rowEnd - col];
        array[rowEnd - col] = temp;
    }
}
点赞