我正在为我的 AP 计算机科学课创建一个井字游戏,到目前为止它运行没有错误,或者通过简单的修复解决了错误。但是,当我向程序添加 if 语句时,它会不断显示错误,指出“无法访问代码”。我仍然无法确定原因或如何解决此问题。
我使用“Magic Square”设置游戏,其中每一行和对角线都设置为一个变量并加起来为 15,这就是程序确定获胜者的方式。
错误出现在这个 if 语句中:
if (topx == 15 || middlex == 15 || bottomx == 15 || leftx == 15 || centerx == 15 || rightx == 15 || diag1x == 15 || diag2x == 15) { String XWIN = ("X 赢了!"); g.drawString(XWIN, 60, 50); }
上面的 if 语句与 x 播放器有关,但 o 播放器的 if 语句也有相同的错误。
整个代码如下所示(我为糟糕的笔记道歉)
public void paint(Graphics g){
this.setSize(450, 430); //sets the game screen size.
//initial directions
System.out.println("Player 1 (x) goes first. there are 9 boxes available.");
System.out.println("The numbers correspond to the boxes respectively.");
System.out.println("1 being top left, 2 being top center, 3 being top right, and so on.");
System.out.print("PLAYER 1 enter location of x: ");
setBackground( Color.black ); //background color
//listed variables assigned to each box, x and o, respectively
int topx = 0;
int middlex = 0;
int bottomx = 0;
int leftx = 0;
int centerx = 0;
int rightx = 0;
int diag1x = 0;
int diag2x = 0;
int topo = 0;
int middleo = 0;
int bottomo = 0;
int lefto = 0;
int centero = 0;
int righto = 0;
int diag1o = 0;
int diag2o = 0;
//board
g.setColor( Color.WHITE );
g.drawRect(50,50,225,225);
g.drawLine(125, 50, 125, 275);
g.drawLine(200, 50, 200, 275);
g.drawLine(50, 125, 275, 125);
g.drawLine(50, 200, 275, 200);
//for loop and implemented scanner
for (int x = 1; x <= 5; x++) {
g.setColor( Color.CYAN);
Scanner keyboard1 = new Scanner(System.in);
int p1 = keyboard1.nextInt();
System.out.print("PLAYER 2 enter location of o: ");
switch (p1){
case 1:
g.drawLine(75, 75, 100, 100);
g.drawLine(100, 75, 75, 100);
topx += 8;
leftx += 8;
diag1x +=8;
break;
case 2:
g.drawLine(150, 75, 175, 100);
g.drawLine(175, 75, 150, 100);
centerx += 1;
topx += 1;
break;
case 3:
g.drawLine(225, 75, 250, 100);
g.drawLine(250, 75, 225, 100);
rightx += 6;
topx += 6;
diag2x += 6;
break;
case 4:
g.drawLine(75, 150, 100, 175);
g.drawLine(75, 175, 100, 150);
middlex += 3;
leftx += 3;
break;
case 5:
g.drawLine(150, 150, 175, 175);
g.drawLine(150, 175, 175, 150);
diag1x += 5;
diag2x += 5;
middlex += 5;
centerx += 5;
break;
case 6:
g.drawLine(225, 150, 250, 175);
g.drawLine(225, 175, 250, 150);
middlex += 7;
rightx += 7;
break;
case 7:
g.drawLine(75, 225, 100, 250);
g.drawLine(75, 250, 100, 225);
diag2x += 4;
leftx += 4;
bottomx += 4;
break;
case 8:
g.drawLine(150, 225, 175, 250);
g.drawLine(150, 250, 175, 225);
bottomx += 9;
centerx += 9;
break;
case 9:
g.drawLine(225, 225, 250, 250);
g.drawLine(225, 250, 250, 225);
bottomx += 2;
rightx += 2;
diag1x += 2;
break;
if ( topx == 15 || middlex == 15 || bottomx == 15 || leftx == 15 || centerx == 15 || rightx == 15 || diag1x == 15 || diag2x == 15 ) {
String XWIN = ("X wins!");
g.drawString(XWIN, 60, 50);
}
g.setColor( Color.GREEN);
Scanner keyboard2 = new Scanner(System.in);
int p2 = keyboard2.nextInt();
System.out.print("PLAYER 1 enter location of x: ");
switch (p2){
case 1:
g.drawOval(75, 75, 25, 25);
topo += 8;
lefto += 8;
diag1o +=8;
break;
case 2:
g.drawOval(150, 75, 25, 25);
centero += 1;
topo += 1;
break;
case 3:
g.drawOval(225, 75, 25, 25);
righto += 6;
topo += 6;
diag2o += 6;
break;
case 4:
g.drawOval(75, 150, 25, 25);
middleo += 3;
lefto += 3;
break;
case 5:
g.drawOval(150, 150, 25, 25);
diag1o += 5;
diag2o += 5;
middleo += 5;
centero += 5;
break;
case 6:
g.drawOval(225, 150, 25, 25);
middleo += 7;
righto += 7;
break;
case 7:
g.drawOval(75, 225, 25, 25);
diag2o += 4;
lefto += 4;
bottomo += 4;
break;
case 8:
g.drawOval(150, 225, 25, 25);
bottomo += 9;
centero += 9;
break;
case 9:
g.drawOval(225, 225, 25, 25);
bottomo += 2;
righto += 2;
diag1o += 2;
break;
if ( topo == 15 || middleo == 15 || bottomo == 15 || lefto == 15 || centero == 15 || righto == 15 || diag1o == 15 || diag2o == 15 ) {
String OWIN = ("O wins!");
g.drawString(OWIN, 60, 50);
}
default : System.out.println("This is not a valid input. Please enter a number 1 through 9.");
}
}
}
}