Java鼠标程序错误输出

问题描述 投票:0回答:1

我正在研究一个应该打印2D数组的Java程序,“鼠标”需要在该数组中随机移动。如果鼠标指针击中数组的边缘,它将测试鼠标触到的数字是-1(鼠标转义数组)还是-2(鼠标死)。如果移动超过50次,它就会死掉(整个过程在for循环中重复了1000次)。每次鼠标移动时,它将用其上的移动编号替换其上的阵列位置编号。示例:总动作是鼠标完成的动作数:

totalMoves++;
island[r][c] = totalMoves;

程序执行后,应打印如下内容:

~~The mouse drowned in the water!<--<@
W   W   B   B   W   B   W   B   W   W   B   W   W   B   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   6   5   0   B   
W   0   0   0   0   0   0   0   0   0   0   7   4   0   W   
W   0   0   0   0   0   0   0   0   0   1   8   9   10  11   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   B   W   B   W   B   B   B   W   B   W   W   W   W   W   

---------------------------------------------------------

FYI,W为-2,B为-1,但打印字符串。问题是鼠标没有向上方打印输出,而是仅向右移动。是的,就像直接向右移动一样:

~~The mouse drowned in the water!<--<@
W   W   B   B   W   B   W   B   W   W   B   W   W   B   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   B   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   1   2   3   4   5   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   0   0   0   0   0   0   0   0   0   0   0   0   0   W   
W   B   W   B   W   B   B   B   W   B   W   W   W   W   W   

---------------------------------------------------------

我尝试阅读代码OVER和OVER,但是找不到解决方案。这是我的程序:对象类别:

import java.util.Random;

public class Mouse {
    final int NUM_ROWS = 10;
    final int NUM_COLS = 15;
    final int MAX_MOVES = 50;
    private int drown = 0;//drown count
    private int starve = 0;//starve count
    private int escape = 0;//escape count
    int[][] island = new int[NUM_ROWS][NUM_COLS];
    Random rand = new Random ();
    int x = 0;
    int y = 0;




    public int getSc()// gets starve count
    {
       return starve;
    }

     public int getDc()// gets drown count
    {
       return drown;
    }

     public int getEc()// gets escape count
    {
       return escape;
   }


    public void createIsland()
    {
       for (int r = 0; r < island.length; r++)
       {
          for (int c = 0; c < island[r].length; c++)
          {
                island[r][c] = 0;           
          }

      }
    }

    public String printIsland()
    {
        String ans = "";
       for (int r = 0; r < island.length; r++)
       {
          for (int c = 0; c < island[r].length; c++)
          {
              if(island[r][c] == -1)
              {
                  ans+= "B   ";

              }
              else if(island[r][c] == -2)
              {
                  ans+= "W   ";
              }
              else
              {
                 ans+= island[r][c] + "   ";     
              }

          }
         ans+="\n";
      }
    return ans;
    }



    public void edgeIsland() 
    {
        for(int r = 0; r<island.length;r++)
        {
            float chance = rand.nextFloat();

              if (chance <= .30f)
              {
                  island[r][0] = -1;
              }
              else
              {
                  island[r][0] = -2;
              }
        }
        for(int r = 0; r<island[0].length;r++)
        {
            float chance = rand.nextFloat();

              if (chance <= 0.30f)
              {
                  island[0][r] = -1;
              }
              else
              {
                  island[0][r] = -2;
              }
        }

        for(int r = 0; r<island[0].length;r++)
        {
            float chance = rand.nextFloat();

              if (chance <= 0.30f)
              {
                  island[9][r] = -1;
              }
              else
              {
                  island[9][r] = -2;
              }
        }
        for(int r = 0; r<island.length;r++)
        {
            float chance = rand.nextFloat();

              if (chance <= 0.30f)
              {
                  island[r][14] = -1;
              }
              else
              {
                  island[r][14] = -2;
              }
        }

    }

    public void randomStart()// where the mouse starts out
    {
       int min1 = 2;
       int max1 = 8;
       int min2 = 2;
       int max2 = 13;
       x = rand.nextInt((max1 - min1) + 1) + min1;
       y = rand.nextInt((max2 - min2) + 1) + min2;
       island[x][y] += 1;
    }
    public void mouseStart()
    {   
        int totalMoves = 0;
        for (int r = x; r < island.length; r++)
           {
              for (int c = y; c < island[r].length; c++)
              {
                  if(totalMoves<MAX_MOVES)
                  {
                       int min1 = 1;
                       int max1 = 4;
                       int move = rand.nextInt((max1 - min1) + 1) + min1;



                        if (move == 1)//up
                         {

                                island[r][c] = island[r][c+1];


                            if (totalMoves == MAX_MOVES)
                            {
                              // System.out.println("________________________" +
                                //  "_________________________");
                               System.out.println("~~The mouse wandered" +
                                  " around and starved!<--<@");
                               starve++;
                               totalMoves = MAX_MOVES+1;
                               break;
                            }
                            else if (island[r][c] == -2)
                            {
                              // System.out.println("________________________" +
                            //"_________________________");
                               System.out.println("~~The mouse drowned in" +
                               " the water!<--<@");
                               drown++;
                               totalMoves++;
                               island[r][c] = totalMoves;
                               totalMoves = MAX_MOVES+1;
                               break;
                            }
                            else if (island[r][c] == -1)
                            {
                              // System.out.println("________________________" +
                            // "_________________________");
                               System.out.println("~~The mouse found a bridge" +
                               " and escaped! :) ");
                               escape++;
                               totalMoves++;
                               island[r][c] = totalMoves;
                               totalMoves = MAX_MOVES+1;
                               break;
                            }
                            else
                               {
                                totalMoves++;
                                  island[r][c] = totalMoves;
                               }
                         }
                            else if (move == 2)//down
                            {

                                    island[r][c] = island[r][c-1];                         


                               if (totalMoves == MAX_MOVES)
                               {
                                 //System.out.println("________________________" +
                                   //  "_________________________");
                                  System.out.println("~~The mouse wandered" +
                                     " around and starved!<--<@");
                                  starve++;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -2)
                               {
                                //  System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse drowned in" +
                                  " the water!<--<@");
                                  drown++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -1)
                               {
                                 // System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse found a bridge" +
                                  " and escaped! :) ");
                                  escape++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else
                               {
                                totalMoves++;
                                  island[r][c] = totalMoves;

                               }
                            }
                            else if (move == 3)//right
                            {

                                    island[r][c] = island[r+1][c];


                               if (totalMoves == MAX_MOVES)
                               {
                                 // System.out.println("________________________" +
                                 //    "_________________________");
                                  System.out.println("~~The mouse wandered" +
                                     " around and starved!<--<@");
                                  starve++;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -2)
                               {
                                //  System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse drowned in" +
                                  " the water!<--<@");
                                  drown++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -1)
                               {
                               //   System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse found a bridge" +
                                  " and escaped! :) ");
                                  escape++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else
                               {
                                totalMoves++;
                                  island[r][c] = totalMoves;
                               }
                            }
                            else//right
                            {
                                island[r][c] = island[r-1][c];
                               if (totalMoves == MAX_MOVES)
                               {
                               //   System.out.println("________________________" +
                               //      "_________________________");
                                  System.out.println("~~The mouse wandered" +
                                     " around and starved!<--<@");
                                  starve++;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -2)
                               {
                                //  System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse drowned in" +
                                  " the water!<--<@");
                                  drown++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else if (island[r][c] == -1)
                               {
                                //  System.out.println("________________________" +
                               // "_________________________");
                                  System.out.println("~~The mouse found a bridge" +
                                  " and escaped! :) ");
                                  escape++;
                                  totalMoves++;
                                  island[r][c] = totalMoves;
                                  totalMoves = MAX_MOVES+1;
                                  break;
                               }
                               else
                               {
                                totalMoves++;
                                  island[r][c] = totalMoves;
                               }
                            }
                  }
              }

          }

    }


}

测试类别:

public class MouseTest {

    public static void main(String[] args) {
        int NUM_SIMULATIONS = 1000;
        for(int c = 0;c<=NUM_SIMULATIONS; c++)
        {
        Mouse g = new Mouse();
        g.createIsland();
        g.edgeIsland();
        g.randomStart();
        g.mouseStart();
        System.out.print(g.printIsland() + "\n---------------------------------------------------------\n\n");
        }




    }

}

[如果有人可以帮助我解决此问题,将不胜感激。谢谢大家!

java arrays
1个回答
1
投票

问题是您遍历了孤岛的所有索引,但是您甚至没有在主循环中使用鼠标的xy。我不确定您要做什么,但是由于c仅在增加,因此鼠标移至右侧也就不足为奇了。

© www.soinside.com 2019 - 2024. All rights reserved.