java - 加载不同的地图

发布于 2024-09-09 01:04:22 字数 1006 浏览 2 评论 0原文

好的,我正在制作这个小程序,我希望它根据数字生成一个世界...

它是:

 public int[][] loadBoard(int map) {
    if (map == 1) { int[][] board = { {
 2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 int[][] board = {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } return board; }

并调用它我使用:

board = loadBoard(1);

我把它放在 init() 方法中。这样我就可以根据 loadBoard() 内的数字调用地图。然而,当我开始游戏时,我得到了空指针异常,并且我知道它与我上面刚刚向您展示的代码有关。这可能是我正在犯的一些菜鸟错误..也许你可以帮忙?

OK so I have this applet I am making and I want it to generator a world according to a number...

Here it is:

 public int[][] loadBoard(int map) {
    if (map == 1) { int[][] board = { {
 2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 int[][] board = {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } return board; }

and to call it I use:

board = loadBoard(1);

I put that in the init() method. Then that way I can call maps upon the number inside loadBoard(). However, when I start my game I get nullpointer exception and I KNOW for a FACT that its something to do with the code I just showed you above. It's probably some rookie mistake I am doing.. maybe you can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

站稳脚跟 2024-09-16 01:04:22

这是。您再次创建“board”变量。即使名称相同,您返回的变量也不是您创建的变量。这是固定的代码:

public int[][] loadBoard(int map) {
    if (map == 1) { 
 return new int[][] { 

 {2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 return new int[][] {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } 

}

如果我现在知道您计划拥有多少个“数字”,它可以进一步简化:)

另一个建议是不要动态创建数组,而是将它们作为常量。然后从该方法返回适当的数组。您的代码可能如下所示(对于两个以上的选择):

 private static final int[][] BOARD1 = <array here>;
 private static final int[][] BOARD2 = <array here>;
 private static final int[][] BOARD3 = <array here>;
 private static final int[][] BOARD4 = <array here>;


 public function int[][] loadBoard( int choice ) {
   switch( choice ) {
      case 1: return BOARD1;
      case 2: return BOARD2;
      case 3: return BOARD3;
      case 4: return BOARD4;
      default: throw new RuntimeException( "Unknown board choice" );
   }
 }

It is. You create "board" variable again. Even though the name is the same the variable you return is not the one you created. Here is the fixed code:

public int[][] loadBoard(int map) {
    if (map == 1) { 
 return new int[][] { 

 {2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 return new int[][] {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } 

}

It can be simp'ified even more if I would now how many "numbers" you're planing to have :)

Another suggestion would be to NOT create arrays on the fly, but have them as constants. Then return appropriate array from the method. Your code may look like (for more then 2 choices):

 private static final int[][] BOARD1 = <array here>;
 private static final int[][] BOARD2 = <array here>;
 private static final int[][] BOARD3 = <array here>;
 private static final int[][] BOARD4 = <array here>;


 public function int[][] loadBoard( int choice ) {
   switch( choice ) {
      case 1: return BOARD1;
      case 2: return BOARD2;
      case 3: return BOARD3;
      case 4: return BOARD4;
      default: throw new RuntimeException( "Unknown board choice" );
   }
 }
如痴如狂 2024-09-16 01:04:22

我不确定你的空指针与这段代码有关。然而,在 if/else 语句的作用域内声明 board 并在作用域外返回此变量的事实确实很奇怪。

I'm not sure that your null pointer is related to this code. However, it is really weird the fact that you declare board inside the scope of the if/else statement and the return this variable outside the scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文