有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中运行带有try/catch块的方法时,在调试模式下出现eclipse Source not found错误

我正在用Java开发一个简单的connect-four游戏,并试图在代码中使用try/catch块,将播放器的光盘放入游戏板。我编辑了一些代码,故意让它在播放机试图在整列中放入光盘时抛出一个错误,但当我进行更改时,eclipse中出现了一个“SourceNotFound”错误。代码如下:

public void dropDisc(int column, int player, Scanner input) {
        //Method to drop a disc in the appropriate column of the game board.

        char disc;

        if(player == 1)
            disc = 'R';
        else
            disc = 'Y';

        column = (column * 2) + 1;  //Sets column to a blank space 

        try{
        //  for(int i = 0; i <= 5; i++) {
        //  
        //      if (i == 5) {
        //          gameBoard[i][column] = disc;
        //          break;}
        //      else if (gameBoard[i + 1][column] == 'Y' || gameBoard[i + 1][column] == 'R') {
        //          gameBoard[i][column] = disc;
        //          break;}
        //  }
            for(int i = 5; i <= -1; i--) {

                if (gameBoard[i][column] == 'Y' || gameBoard[i][column] == 'R')
                    continue;
                else
                    gameBoard[i][column] = disc;
            }
        } catch(Exception e) {
            System.out.println("Column full, please select another column.");
            column = input.nextInt();

            dropDisc(column, player, input);
        }
    }

如果我注释掉第二个for循环并取消注释第一个for循环,那么代码工作正常。我不确定在使用第二个版本的循环时为什么会出现这个源未找到错误。这里有什么明显的我遗漏的吗


共 (1) 个答案

  1. # 1 楼答案

    很难识别问题,因为您只发布了部分代码。如果你把所有的东西都贴出来,帮助你会更容易。尽管如此:

    除了缺少一个结尾“}”,您的代码对我来说编译得很好

    但是,此循环永远不会执行:

    for(int i = 5; i <= -1; i ) {
    

    你可能想要更接近这一点的东西:

    for(int i = 5; i >= 0; i ) {