方法未调用,调试消息未显示

时间:2013-12-02 13:10:50

标签: java methods indentation

基本上,一切都是隐居的,但由于某种原因,在我的generateBoard方法结束时,它实际上并没有调用方法(printBoard)来打印它。我相信这应该可以工作,但即使我的调试信息突然出现,所以我想我某处有缩进错误?

import java.util.Random;
import java.util.Scanner;

public class Zombies {
    private int Level = 1;
    private int MoveNo = 0;
    public static char[][] myGrid = new char[12][12];

        public static void levelInfection() {
            Scanner input = new Scanner (System.in);
            System.out.println("How many infected tiles would you like to start the game with?");
            if(input.hasNextInt()) {
                int nLevel = input.nextInt();
                if(nLevel > 6) {
                    System.out.println("You can't have that many infected tiles, you'd never survive!");
                    levelInfection();
                }
                else if (nLevel < 0){
                    System.out.println("You need to have some infected tiles, and don't try any negative numbers!");
                    levelInfection();
                }
                else if (nLevel < 6 && nLevel > 0) {
                    System.out.println("We will now create your gameboard with " + nLevel + " infected tiles, good luck!");
                    generateBoard(nLevel);
                }
            }
            else {
                System.out.println("I'm sorry, you didn't enter an integer number. Please try again.");
                levelInfection();
            }
        }

    public static void generateBoard(int nLevel) {
        Random rand = new Random();
        int i, j;
        int infTile = 0;

        for(i = 0; i < 12; i++) {
            for(j = 0; j < 12; j++) {
                if (i == 6 && j == 6) {
                    myGrid[i][j] = 'P';
                    System.out.println("I made a player tile");
                }
                else if(rand.nextInt(9) == 0) {
                    if(myGrid[i][j] == 'I'||infTile >= nLevel) {
                        System.out.println("I found infected tile");
                        return;
                    }
                    else {
                        myGrid[i][j] = 'I';
                        System.out.println("I made an infected tile");
                        infTile++;
                        System.out.println("I counted an infected tile (" + infTile +") at " + i + "," + j);
                    }
                }
                else {
                    myGrid[i][j] = 'x';
                }
            }
        }
        System.out.println("Print me mofo!");
        printBoard();
    }

    public static void printBoard() {
        int i, j;
        for(i = 0; i < 12; i++) {
            for(j = 0; j < 12; j++) {
                if(j == 0) {
                    System.out.print( "| " );
                }
                System.out.print( myGrid[i][j] + " " );
                if(j == 11) {
                    System.out.print( "|\n" );
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

也许是在点击你的return声明

  if(myGrid[i][j] == 'I'||infTile >= nLevel) {
    System.out.println("I found infected tile");
    return;
  }

...如果没有接到电话就退出方法。

答案 1 :(得分:1)

当您找到受感染的磁贴时,您已在执行顺序中放置了一个return语句。这会将控件返回给调用函数,从而终止执行generateBoard()

如果您在遇到受感染的磁贴时需要该数组的值,则应该使用break;语句。这将打破当前的for循环,但函数的其余部分仍将执行。

希望这有帮助。