繁体   English   中英

无法弄清楚为什么我在我的数组print语句中获取空值

[英]Can't figure out why im getting null values in my array print statement

我正在加载一个数组,从它周围的值中获取总和,然后根据sum / 6的值为一个新数组赋值。 因此,当我运行它时,每个数组值都应包含一些内容,而不是null。 但是,当我打印我的数组时,我得到了预期的输出(基于我给出的一个例子),除了我的print语句中的随机“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
                                       .             . * . .    

这是应该输出的:

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

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

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

因此,您可以看到它基本上是相同的东西减去空值,并且如果没有空占用空间,某些字符将正确格式化。

在设置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]="  ";
}

您的代码无法处理一些边缘情况。 没有条件是true ,因此mapicons不是完全填满的。 然后,打印出null值。

例如,当sum恰好等于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 chain。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM