gpt4 book ai didi

java - 使用 try/catch block 在 Java 中运行方法时,在 Debug模式下给出源未找到错误

转载 作者:行者123 更新时间:2023-12-01 23:16:40 26 4
gpt4 key购买 nike

我正在用 Java 开发一个简单的四连棋游戏,并尝试在我的代码中使用 try/catch block ,将玩家的光盘放入游戏板中。我编辑了一些代码,故意让它在播放器尝试将光盘放入完整列中时抛出错误,但是当我进行更改时,我在 Eclipse 中收到“未找到源”错误。代码如下:

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(int i = 5; i <= -1; i--) {

您可能想要更接近此的东西:

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

关于java - 使用 try/catch block 在 Java 中运行方法时,在 Debug模式下给出源未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58349158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com