无法访问的代码,if语句,在tic tac toe?

时间:2014-01-13 18:34:52

标签: java compiler-errors applet awt unreachable-code

我正在为我的AP计算机科学课创建一个tic tac toe游戏,到目前为止它没有错误地运行,或者错误通过简单的修复解决了。但是,当我向程序添加if语句时,它会不断显示一条错误,指出“无法访问的代码”。我仍无法确定原因或如何解决此问题。

我使用“魔方”设置游戏,每行和对角线设置为一个变量,最多加15,这就是程序确定胜利者的方式。

此if语句出现错误:

  

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); }

以上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.");

                }

            }

        }
    }

2 个答案:

答案 0 :(得分:5)

出于天堂的缘故,它就在休息之后。中断强制代码流结束并离开当前块,因此从未到达块中的任何代码。所以编译器正在抱怨。不要这样做。

您的帖子中还有许多其他有问题的代码,包括在绘图方法中使用扫描仪。

您应该废弃此代码,并阅读Swing教程和常规Java教程。首先,您几乎不应该覆盖paint方法,而是覆盖JComponent或JPanel的paintComponent方法。对于另一种方法,这些方法应该仅用于绘画和绘画,并且永远不应该被扫描仪阻挡。这只会冻结你的GUI,使它无用。

我会重新构建这个程序。

  • 我不会在GUI程序中使用扫描仪。 GUI程序本质上是事件驱动,而Scanner类型代码是完全不兼容的线性控制台代码。
  • 而是在使用GridLayout的JPanel中保存的JLabel上使用MouseListener等侦听器。
  • 如果我画了任何东西,那么它将在JPanel中的paintComponent覆盖中。
  • 但我会尝试避免绘画,而是在我的JLabel中交换带有“X”,“O”或空白图像的ImageIcons,具体取决于它们的状态。

修改:由于您必须使用AWT,因此对recs进行了一些更改:

  • 但是,出于同样的原因,请不要使用Scanner。
  • 仍然使用事件驱动的编程,例如附加到Labels的MouseListener。
  • 如果您需要自定义图形,请覆盖绘制方法。
  • 同样,此方法中不应包含程序逻辑和阻塞代码。它应该仅用于绘图和绘图。

答案 1 :(得分:2)

该错误与您的if声明的内容无关;您在if语句之后以及之后的break之前放置case语句 - 程序执行无法达到该语句。 (这就是'无法访问的代码'的含义 - 通常,当您看到该错误时,您需要退后一步并查看上下文,并尝试找出应该到达该行的输入集 - 通常,当你试图解决这个问题时,你会明白为什么它不会发生。)