【问题标题】:Loops, 2D arrays循环、二维数组
【发布时间】:2017-04-28 00:23:17
【问题描述】:

为什么这个程序会打印以下输出?

import java.util.Arrays;
import java.util.Collections;

public class FifteenModel implements Boardgame
{


  private String currentMessage = "No message yet";
  private String[][] status = new String[4][4];  // spelplanen
  private int iemp, jemp;   



  public void FiftenModel()
  {
      createGameBoard();
  }

  private void createGameBoard()
  {
      String[] rutor = new String[16];  //Skapar matrisen
      for (int i = 1; i <= 16; i++)
      {
          rutor[i-1] = String.valueOf(i);
          if (i == 16)
          {
              rutor[i-1] = null;
          }
      }
      shuffleGameBoard(rutor);
  }

  private void shuffleGameBoard(String[] rutor)
  {
    Collections.shuffle(Arrays.asList(rutor));  
    saveArray(rutor);
  }



  private void saveArray(String[] rutor)
  {
      for (int i = 0; i < 4; i++)
      {
          for (int j = 0; j < 4; j++)
          {
            status[i][j] = rutor[4 * i + j ];

            if (status[i][j] == null)
            {
                status[i][j] = "";
                iemp = i;
                jemp = j;
            }
          }
      }
  }


  public boolean move(int i, int j)
  {

      if (((iemp - 1 <= i) && (i <= iemp + 1)) && ((jemp - 1 <= j) && (j <= jemp + 1)))

          if ((iemp == i) ^ (jemp == j))
          {
              currentMessage = "Elements is changed!";
              status[iemp][jemp] = status[i][j];
              status[i][j] = "";
              iemp = i;
              jemp = j;
              return true;

          }
        currentMessage = "Choose the right element!";
        return false;     

  }

  public String getStatus(int i, int j){
      return status[i][j];
  }


  public String getMessage(){
      return currentMessage;
  }


  public boolean gameDone(){

      for (int j = 0; j < 4; j++)
          for (int i = 0; i < 4; i++)
            try {
                if (Integer.valueOf(getStatus(i,j)) == (j * 4 + i + 1))
                {
                    currentMessage = "Congrats!";
                    return true;
                }
            }   catch (Exception e){
            }
     return false;
  }

}

有了这个输出:

Welcome to fifteen puzzle

  null  null  null  null
  null  null  null  null
  null  null  null  null
  null  null  null  null

Your move: 

为什么它是空的?假设为 1 到 15,最后一个为空,然后程序假设对该矩阵进行洗牌。现在,我认为问题出在 shuffle 方法上,但由于我是初学者,所以很难找到它。

!编辑!另一个文件。

class Text15 {
    public static void main(String[] u) {
        Scanner scan = new Scanner(System.in);
        Boardgame thegame = new FifteenModel();                 // Model object is created
        System.out.println("\nWelcome to fifteen puzzle\n");
        while (true) {
            // Print the current board
            for (int i=0; i<4; i++) {
                for (int j=0; j<4; j++)
                    System.out.print("  " + thegame.getStatus(i,j)); // getStatus
                System.out.println();
            }
            System.out.println();
            System.out.print("Your move: ");
            int i = scan.nextInt();  // get an int number from terminal window
            int j = scan.nextInt();
            thegame.move(i,j);                               // move
            System.out.println(thegame.getMessage());        // getMessage
        }
    }
}

【问题讨论】:

  • 你的主要方法在哪里?调试器告诉你什么?
  • public void FiftenModel() 不是构造函数,顺便说一句
  • 没什么,它只是打印出我发布的内容。有问题吗?
  • 你已经包含了一堆代码,但是没有一个打印出你在输出中显示的消息?
  • 看不到 Welcome to fifteen puzzleYour move: - 这真的是代码吗?

标签: java arrays loops output


【解决方案1】:

你已经创建了一个名为FiftenModel的方法

但你想要的是 this 作为构造函数

public FifteenModel () // fix spelling mistake as well
{
  createGameBoard();
}

还可以尝试调试您的代码,以确定 status[i][j] = rutor[4 * i + j ]; 是否正确,因为它看起来会出现越界异常

【讨论】:

  • 我有点脑残,我觉得我不需要构造函数?
  • 因为createGameBoard(); 是私有的,除非你改变它,那么最好的地方可能是从构造函数中调用它,我已经在上面提供了
  • 是的,现在!哇!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 2016-06-28
相关资源
最近更新 更多