Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java鼠标程序输出错误_Java_Arrays - Fatal编程技术网

Java鼠标程序输出错误

Java鼠标程序输出错误,java,arrays,Java,Arrays,我正在开发一个java程序,该程序应该打印一个2D数组,“鼠标”需要在数组中随机移动。如果鼠标击中数组的边缘,它将测试它击中的数字是-1(鼠标逃逸数组)还是-2(鼠标死亡)。如果移动超过50次,它就会死亡(整个过程在for循环中重复1000次)。每次鼠标移动时,它都会将其上的阵列位置编号替换为其上的移动编号。示例:total moves是鼠标完成的移动次数: totalMoves++; island[r][c] = totalMoves; 程序执行后,应按如下方式打印最后3行: # of ti

我正在开发一个java程序,该程序应该打印一个2D数组,“鼠标”需要在数组中随机移动。如果鼠标击中数组的边缘,它将测试它击中的数字是-1(鼠标逃逸数组)还是-2(鼠标死亡)。如果移动超过50次,它就会死亡(整个过程在for循环中重复1000次)。每次鼠标移动时,它都会将其上的阵列位置编号替换为其上的移动编号。示例:total moves是鼠标完成的移动次数:

totalMoves++;
island[r][c] = totalMoves;
程序执行后,应按如下方式打印最后3行:

# of times the mouse escaped: 222
# of times the mouse drowned: 494
# of times the mouse starved: 284

这些数字的总和也应达到1000(最大模拟) 仅供参考,W为-2,B为-1,但打印的是字符串。 问题在于,它不打印上面的输出,而是将存储的时间量打印为0、1或2:

# of times the mouse escaped: 222
# of times the mouse drowned: 494
# of times the mouse starved: 0
我试着反复阅读代码,但找不到解决方案。这是我的节目: 对象类:

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;
    private int starve = 0;
    private int escape = 0;
    int[][] island = new int[NUM_ROWS][NUM_COLS];
    Random rand = new Random ();
    int x = 0;
    int y = 0;




    public int getStarve()
    {

       return starve;
    }

     public int getDrown()
    {
       return drown;
    }

     public int getEscape()
    {
       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 if(island[r][c]>=10)
              {
                 ans+= island[r][c] + "  ";     
              }
              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;
              }
        }

    }
    int start = 0;
    public void randomStart()
    {

        start = island[4][7] += 1;
    }

        String ans = "";        
        public String getAns()
        {

            return ans;
        }

          public void mouseMov()
          {
            int moveCount = start;


             int move = rand.nextInt((4 - 1) + 1) + 1;

             for(int k = 0; k < 20; k++)
             {
                for (int i = 0; i < island.length; i++)
                {
                   for (int j = 0; j < island[i].length; j++)
                   {
                      if (island[i][j] == moveCount && moveCount <= MAX_MOVES)
                      {
                          move =  rand.nextInt((4 - 1) + 1) + 1;


                         if (move == 1)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j-1] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i][j-1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j-1] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i][j-1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i][j-1] = moveCount;
                            }
                         }
                         else if (move == 2)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                              " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j+1] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i][j+1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j+1] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i][j+1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i][j+1] = moveCount;

                            }
                         }
                         else if (move == 3)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                                  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i-1][j] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i-1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i-1][j] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i-1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i-1][j] = moveCount;
                            }
                         }
                         else
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                                  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i+1][j] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i+1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i+1][j] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i+1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i+1][j] = moveCount;
                            }
                         }
                      }
                  }
              }
          }
     }
}
import java.util.Random;
公共类鼠标{
最终整数行数=10;
最终整数=15;
最终整数最大移动=50;
私人int溺水=0;
私有整数饥饿=0;
私有int-escape=0;
int[][]岛=新int[NUM_ROWS][NUM_COLS];
Random rand=新的Random();
int x=0;
int y=0;
public int get饥饿()
{
回归饥饿;
}
公共int getcrown()
{
回归溺水;
}
公共int getEscape()
{
返回逃逸;
}
公共岛()
{
对于(int r=0;r=10)为else
{
ans+=岛[r][c]+';
}
其他的
{
ans+=岛[r][c]+';
}
}
ans+=“\n”;
}
返回ans;
}
公共空间边缘地带()
{

对于(int r=0;r来说,问题是你浏览了岛上的所有索引,但你甚至没有在主循环中使用鼠标的
x
y
。我不知道你想做什么,但随着
c
的增加,鼠标向右移动也就不足为奇了


就我个人而言,我认为这个游戏太大了,不能只是摆弄一个大数组。不管是谁给你们这个任务,都是在向你们收费。这里是一个缺少游戏逻辑的类设计

请注意,
Board
Position
是不可变的类,它们不改变状态(它们的字段内容在创建后是固定的)

公共期末考试委员会{
桥的专用静态最终浮动概率=0.3F;
公共枚举块{
路面,
水,,
桥梁;
}
私人最终瓷砖[][]瓷砖;
私有最终int xMax;
私人终审法院;
私有最终随机rng;
公用板(内部宽度、内部高度){
this.tiles=null;
这是0.xMax=0;
此值为0.yMax=0;
this.rng=null;
设置板();
}
私人发展局(){
//创建瓷砖
}
私人瓷砖选择器BridgeorWater(){
//待办事项实施
返回null;
}
私有布尔OneEdge(整数x,整数y){
//待办事项实施
返回false;
}
公共磁贴getTile(位置){
//待办事项实施
返回null;
}
公共int getWidth(){
//待办事项实施
返回0;
}
公共整数getHeight(){
//待办事项实施
返回0;
}
}
公共枚举方向{
左,右,上,下;
}
公共最终类鼠标{
公共枚举状态{
活着的
逃脱,
溺水,
饥饿;
}
私有LinkedList trail=新LinkedList();
私营国家;
公用鼠标(位置开始位置){
this.trail.add(起始位置);
this.state=state.ALIVE;
}
公共无效移动到(职位新职位){
this.trail.add(newPosition);
}
公共位置getPosition(){
返回trail.getLast();
}
公共无效设置状态(状态){
this.state=状态;
}
公共状态getState(){
返回状态;
}
公共列表{
返回集合。不可修改列表(trail);
}
@凌驾
公共字符串toString(){
返回String.format(“鼠标位于%s处的%s”,getState(),getPosition());
}
}
公务舱鼠标{
私有静态final int NUM_ROWS=10;
私有静态最终整数=15;
私有静态最终整数最大移动=50;
私有随机rng=新随机();
私人董事会;
私人老鼠;
私人整数回合;
私密位置;
私有列表移动;
公共鼠标(){
}
公共作废设置(){
线路板=新线路板(行数、列数);
initialMousePosition=CreateRandomPositionArea(1,board.getWidth()-1,1,board.getHeight()-1);
鼠标=新鼠标(初始鼠标位置);
initialMousePosition=mouse.getPosition();
moves=新建LinkedList();
}
公共位置createRandomPositionInArea(int-xLo、int-xHi、int-YO、int-yHi){
返回null;
}
私人方向随机方向(){
//待办事项实施
返回null;
}
专用位置移动到(位置旧位置,方向){
//托多小鬼
public class MouseTest {

    public static void main(String[] args) {
        int NUM_SIMULATIONS = 1000;
        Mouse g = new Mouse();
        int count = 0;
        for(int c = 0;c<NUM_SIMULATIONS; c++)
        {
        g.createIsland();
        g.edgeIsland();
        g.randomStart();
        g.mouseMov();
        System.out.println(g.getAns());
        System.out.print(g.printIsland() + "\n---------------------------------------------------------\n\n");
        count++;
        }
        System.out.println(count);
        System.out.println("# of times the mouse escaped: " + g.getEscape());
        System.out.println("# of times the mouse drowned: " + g.getDrown());
        System.out.println("# of times the mouse starved: " + g.getStarve());


    }

}

public final class Board {

    private static final float CHANCE_OF_BRIDGE = 0.3F;

    public enum Tile {
        PAVEMENT,
        WATER,
        BRIDGE;
    }

    private final Tile[][] tiles;
    private final int xMax;
    private final int yMax;

    private final Random rng;

    public Board(int width, int heigth) {
        this.tiles = null;
        this.xMax = 0;
        this.yMax = 0;
        this.rng = null;

        setupBoard();
    }

    private void setupBoard() {
        // creates the tiles
    }

    private Tile chooseBridgeOrWater() {
        // TODO implement
        return null;
    }

    private boolean onEdge(int x, int y) {
        // TODO implement
        return false;
    }

    public Tile getTile(Position position) {
        // TODO implement
        return null;
    }

    public int getWidth() {
        // TODO implement
        return 0;
    }

    public int getHeight() {
        // TODO implement
        return 0;
    }
}

public enum Direction {
    LEFT, RIGHT, UP, DOWN;
}

public final class Mouse {

    public enum State {
        ALIVE,
        ESCAPED,
        DROWNED,
        STARVED;
    }

    private LinkedList<Position> trail = new LinkedList<Position>();
    private State state;


    public Mouse(Position startingPosition) {
        this.trail.add(startingPosition);
        this.state = State.ALIVE;
    }

    public void moveTo(Position newPosition) {
        this.trail.add(newPosition);
    }

    public Position getPosition() {
        return trail.getLast();
    }

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public List<Position> getTrail() {
        return Collections.unmodifiableList(trail); 
    }

    @Override
    public String toString() {
        return String.format("Mouse is %s at %s", getState(), getPosition());
    }
}

public class MouseGame {
    private static final int NUM_ROWS = 10;
    private static final int NUM_COLS = 15;
    private static final int MAX_MOVES = 50;

    private Random rng = new Random();
    private Board board;
    private Mouse mouse;

    private int round;
    private Position initialMousePosition;
    private List<Direction> moves;

    public MouseGame() {

    }

    public void setup() {
        board = new Board(NUM_ROWS, NUM_COLS);
        initialMousePosition = createRandomPositionInArea(1, board.getWidth() - 1, 1, board.getHeight() - 1);
        mouse = new Mouse(initialMousePosition);
        initialMousePosition = mouse.getPosition();
        moves = new LinkedList<Direction>();
    }

    public Position createRandomPositionInArea(int xLo, int xHi, int yLo, int yHi) {
        return null;
    }

    private Direction randomDirection() {
        // TODO implement
        return null;
    }

    private Position moveTo(Position oldPosition, Direction direction) {
        // TODO implement
        return null;
    }

    public boolean performRound() {
        // TODO implement
        return false;
    }

    public void printBoard(PrintStream out) {
        // prints the board in the current state (never implemented this)
        // but you go over all the positions in the board
        // and then check if the mouse or trail has been there and print that instead of the "tile"
    }

    public Mouse getMouse() {
        return mouse;
    }


    private static Mouse.State performGame() {
        MouseGame game = new MouseGame();
        game.setup();
        boolean finished = false;
        while (!finished) {
            finished = game.performRound();
        }

        return game.getMouse().getState();
    }


    public static void main(String[] args) {
        int drownCount = 0;
        int starveCount = 0;
        int escapeCount = 0;

        int games = 1_000_000_000;

        var start = System.currentTimeMillis();
        for (int i = 0; i < games; i++) {
            Mouse.State finalState = performGame();
            switch (finalState) {
            case DROWNED:
                drownCount++;
                break;
            case ESCAPED:
                escapeCount++;
                break;
            case STARVED:
                starveCount++;
                break;
            default:
                throw new IllegalStateException();
            }
        }
        var end = System.currentTimeMillis();

        System.out.printf("Escaped: %d, Drowned: %d, Starved: %d%n", escapeCount, drownCount, starveCount);
        System.out.printf("Running time in ns per game (including board setup): %d%n", ((end - start) * 1_000_000) / games);

    }
}

package com.stackexchange.so.mouse.show;

public final class Position {
    private final int x;
    private final int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return String.format("(%d, %d)", x, y);
    }
}
import java.util.Random;

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





    public double getStarve()
    {
        double starveF = starve * 10/100;
        return starveF;
    }

     public double getDrown()
    {
         double drownF = drown * 10/100;
       return drownF;
    }

     public double getEscape()
    {
         double escapeF = escape * 10/100;
       return escapeF;
   }


    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 if(island[r][c] == -3)
              {
                  ans+= "W   ";
              }
              else if(island[r][c] == -4)
              {
                  ans+= "B   ";
              }
              else if(island[r][c]>=10)
              {
                 ans+= island[r][c] + "  ";     
              }
              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;
              }
        }

    }
    int start = 0;
    public void randomStart()
    {

        start = island[4][7] += 1;
    }

        String ans = "";        
        public String getAns()
        {

            return ans;
        }

          public void mouseMov()
          {
            int moveCount = start;


             int move = rand.nextInt((4 - 1) + 1) + 1;

             for(int k = 0; k < MAX_MOVES; k++)
             {
                for (int i = 0; i < island.length; i++)
                {
                   for (int j = 0; j < island[i].length; j++)
                   {
                      if (island[i][j] == moveCount && moveCount <= MAX_MOVES)
                      {
                          move =  rand.nextInt((4 - 1) + 1) + 1;


                         if (move == 1)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j-1] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i][j-1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i][j-1] = -3;
                               break;
                            }
                            else if (island[i][j-1] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i][j-1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i][j-1] = -4;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i][j-1] = moveCount;
                            }
                         }
                         else if (move == 2)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                              " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i][j+1] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i][j+1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i][j+1] = -3;
                               break;
                            }
                            else if (island[i][j+1] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i][j+1] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i][j+1] = -4;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i][j+1] = moveCount;

                            }
                         }
                         else if (move == 3)
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                                  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i-1][j] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i-1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i-1][j] = -3;
                               break;
                            }
                            else if (island[i-1][j] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i-1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i-1][j] = -4;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i-1][j] = moveCount;
                            }
                         }
                         else
                         {
                            if (moveCount == MAX_MOVES)
                            {

                               ans = "The mouse wandered" +
                                  " around and starved!";
                               starve++;
                               moveCount = MAX_MOVES+1;
                               break;
                            }
                            else if (island[i+1][j] == -2)
                            {

                               ans = "The mouse drowned in" +
                               " the water!";
                               drown++;
                               moveCount++;
                               island[i+1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i+1][j] = -3;
                               break;
                            }
                            else if (island[i+1][j] == -1)
                            {

                               ans = "The mouse found a bridge" +
                               " and escaped!";
                               escape++;
                               moveCount++;
                               island[i+1][j] = moveCount;
                               moveCount = MAX_MOVES+1;
                               island[i+1][j] = -4;
                               break;
                            }
                            else
                            {
                                moveCount++;
                               island[i+1][j] = moveCount;
                            }
                         }
                      }
                  }
              }
          }
     }
}
public class MouseTest {

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

        System.out.println("# of times the mouse escaped: " + g.getEscape()+"%");
        System.out.println("# of times the mouse drowned: " + g.getDrown()+"%");
        System.out.println("# of times the mouse starved: " + g.getStarve()+"%");

    }

}