-2

请帮忙,我必须为我的编程课制作一个带有南瓜灯脸的 20 x 20 星号网格。我似乎无法将其放入网格中。它以一条长长的垂直线打印出来,脸部的各个部分随机排列在一起。这是我到目前为止所拥有的:

public class DottinoN_ExtraCredit
{
  public static void main (String [] args)
  {
    int row, col;
    for(row = 0; row < 20; row++)
    {
      for(col = 0; col < 20; col++)
      {
        if((row == 2) && (col == 2) ||(row == 2) && (col == 3) || (row == 2) && (col == 4) || (row == 3) && (col == 2)
             || (row == 3) && (col == 4) || (row == 4) && (col == 2) ||(row == 4) && (col == 3) || (row == 4) && (col == 4)
             || (row == 2) && (col == 15) ||(row == 2) && (col ==16) || (row == 2) && (col == 17) 
             || (row == 3) && (col == 15) || (row == 3) && (col == 17) 
             || (row == 4) && (col == 15) ||(row == 4) && (col == 16) || (row == 4) && (col == 17))
        {
          System.out.print("O");
        }
        else
        {
          if((row == 7) && (col == 9) || (row == 7) && (col == 10) || (row == 8) && (col == 8) || (row == 8) 
               && (col == 9) || (row == 8) && (col == 10) || (row == 8) && (col == 11)) 
          {
            System.out.print("X");
          }
          else
          {
            if((row == 13) && (col == 5) || (row == 13) && (col == 6) || (row == 13) && (col == 7) || 
               (row == 13) && (col == 8) || (row == 13) && (col == 9) || (row == 13) && (col == 10) || 
               (row == 13) && (col == 11) || (row == 13) && (col == 12) || (row == 13) && (col == 13) || 
               (row == 13) && (col == 14) || (row == 14) && (col == 5) || (row == 14) && (col == 6) || 
               (row == 14) && (col == 7) || (row == 14) && (col == 8) || (row == 14) && (col == 9) || 
               (row == 14) && (col == 10) || (row == 14) && (col == 11) || (row == 14) && (col == 12) || 
               (row == 14) && (col == 13) || (row == 14) && (col == 14))
            {
              System.out.println("|");
            }
            else
            {
              System.out.print("*");
          }
          }
        }
        System.out.println("");
      }
    }
  }
}
4

3 回答 3

1

尝试更改System.out.println("|");System.out.print("|");-您真的不希望那里有换行符。

于 2013-10-29T21:21:15.627 回答
0

为什么这么复杂和不舒服?

public void printFigure() {

String[] l = new String[20];
int p = 0;
//       01234567890123456789
l[p++]= "XOOOO----OOOOOOOOOX";
l[p++]= "XOOO(....)OOOOOOOOX";
l[p++]= "XOOOO-----OOOOOOOOX";
l[p++]= "XOOOOXXXOOOOOOOOOOX";
// Add the remaining lines 
 ---
for (int i = 0; i < l.length; i++) {
  System.out.println(l[i];
}
}

但希望数组是允许的,否则这个编程任务没有意义。

于 2013-10-29T21:20:06.920 回答
0

最后一个System.out.println("");在列的末尾执行。基本上,您将每一列放在一个新行中。这就是为什么你会得到一条巨大的垂直线。

于 2013-10-29T21:24:02.203 回答