0

我正在加载一个数组,从它周围的值中获取总和,然后根据 sum/6 将字符串值分配给一个新数组。所以当我运行它时,每个数组值都应该有一些东西,而不是空值。然而,当我打印我的数组时,我得到了预期的输出(基于我给出的一个例子),除了我的打印语句中的随机“null”。我不明白为什么会出现这些空语句。这是我的代码以及打印输出和应该打印输出的示例。

public static void displaymap(int maparray[][])
  {
    String mapicons[][] = new String [22][32];
    int col=1;
    int row=1;
    int row2=0;
    int col2=0;
    double sum[]= new double [704];
    for(int i=0;i<704;i++)
    {
      if(col ==33)
      {
        col = 1;
        row++;
        if(row==24)
        {
          row=0;
          col=0;
          break;
        }
      }
      sum[i]=(double)(maparray[row][col]+maparray[row-1][col]+maparray[row+1][col]+maparray[row][col-1]+maparray[row][col+1])/6;
      col++;
      System.out.printf("%.2f\n",sum[i]);
      if(sum[i]>9)
        {
          mapicons[row2][col2]=" #";
        }
      if(sum[i]>8&&sum[i]<9)
        {
          mapicons[row2][col2]=" *";
        }
      if(sum[i]>7&&sum[i]<8)
        {
          mapicons[row2][col2]=" +";
        }
      if(sum[i]>6&&sum[i]<7)
        {
          mapicons[row2][col2]=" .";
        }
      if(sum[i]<6)
        {
          mapicons[row2][col2]="  ";
        }
      col2++;
      if(col2 ==32)
        {
          col2 = 0;
          row2++;
         }
    }
    for(row=0;row<22;row++)
      {
        for(col=0;col<32;col++)
          System.out.printf("%s",mapicons[row][col]);
        System.out.println();
      }
  }

这是正在输出的内容:

                              null         .                      
        null     #                                          null    
      null .                     +                           . . +
 .     +null                  null   . + .                       .  
         .                         .                            
                                   +     .       .null        null  
                             +             .      null .          
 +                                       .null +     .            
               .        null               . . .    null            
                           .                       .       . .  
                                                 .    null +      
                                            nullnull                
                   .         .                 .       . . .    
     .                                                  nullnull    
                                                       .null      
null     .         .             .                                
 .   .           .           +   .                      null      
  null                          null                                
             .                                 .                
     .               +                           . .            
            null     .         . .                     .   .    null
                                       .             . * . .    

这就是应该输出的内容:

                                       .                      
             #                                              
       .                     +                           . . +
 .     +                     . + .                       .  
         .                         .                            
                                   +     .       .          
                             +             .       .          
 +                                       . +     .            
               .                       . . .                
                           .                       .      . .  
                                                 .     +      

                   .         .                 .       . . .    
     .                                                      
                                                       .      
     .         .             .                                
 .   .           .           +   .                            

             .                                 .                
     .               +                           . .            
                .         . .                     .   .    
                                       .             . * . .    

因此,正如您所看到的,减去空值基本上是一样的,如果没有空值占用空间,某些字符的格式会正确。

4

2 回答 2

1

看起来问题必须在设置 mapicons 值时处理 < 和 > 。您有 > 8 和 < 8 的案例,但 == 8 没有案例(例如)。

试试这个小改变:

if(sum[i]>9)
{
     mapicons[row2][col2]=" #";
}
if(sum[i]>=8&&sum[i]<9)
{
     mapicons[row2][col2]=" *";
}
if(sum[i]>=7&&sum[i]<8)
{
     mapicons[row2][col2]=" +";
}
if(sum[i]>=6&&sum[i]<7)
{
     mapicons[row2][col2]=" .";
}
if(sum[i]<6)
{
     mapicons[row2][col2]="  ";
}
于 2013-11-01T20:59:51.580 回答
1

有一些你的代码无法处理的极端情况。没有条件true,因此mapicons没有完全满足。然后,null值被打印出来。

例如,whensum正好等于 8、7 或 6。

尝试使用以下代码:

public static void displaymap(int maparray[][])
  {
    String mapicons[][] = new String [22][32];
    int col=1;
    int row=1;
    int row2=0;
    int col2=0;
    double sum[]= new double [704];
    for(int i=0;i<704;i++)
    {
      if(col ==33)
      {
        col = 1;
        row++;
        if(row==24)
        {
          row=0;
          col=0;
          break;
        }
      }
      sum[i]=(double)(maparray[row][col]+maparray[row-1][col]+maparray[row+1][col]+maparray[row][col-1]+maparray[row][col+1])/6;
      col++;
      System.out.printf("%.2f\n",sum[i]);
      if(sum[i]>=9)
        {
          mapicons[row2][col2]=" #";
        }
      else if(sum[i]>=8)
        {
          mapicons[row2][col2]=" *";
        }
      else if(sum[i]>=7)
        {
          mapicons[row2][col2]=" +";
        }
      else if(sum[i]>=6)
        {
          mapicons[row2][col2]=" .";
        }
      else
        {
          mapicons[row2][col2]="  ";
        }
      col2++;
      if(col2 ==32)
        {
          col2 = 0;
          row2++;
         }
    }
    for(row=0;row<22;row++)
      {
        for(col=0;col<32;col++)
          System.out.printf("%s",mapicons[row][col]);
        System.out.println();
      }
  }

编辑:您甚至可以使用if/elseif而不是if链。

于 2013-11-01T21:00:12.050 回答