有 Java 编程相关的问题?

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

如果值相同,则停止while循环

基本上,我有一个while循环,它检索被按下的按钮(这是一个棋子)的坐标,并通过这个循环查看是否可以移动

然而,同一国际象棋棋子的坐标不断地被输入,因此,如果棋子没有可用的移动,相同的消息会反复显示,直到按下不同的按钮,如果棋子没有移动,同样的事情会再次发生。 我该如何在每次点击一个页面时只打印一次相同的消息

我尝试使用do while循环,其中while循环中的条件是,如果坐标值与输入的坐标值不同,则只检查按钮是否有可用的移动。但这没什么区别吗

        while(!correctMove){
            do{
                fromXCo = s.getFromXInt();
                fromYCo = s.getFromYInt();
                toXCo = s.getToXInt();
                toYCo = s.getToYInt();

                    p = board.getPiece(fromXCo, fromYCo);

                    //checks if occupied co-ordinate, if not goes back out the loop and asks again.
                    if (!board.occupied(fromXCo, fromYCo)){
                        System.out.println("Please choose occupied co-ordinates \n");
                        correctMove = false;
                    }

                    //checks if the colour is the same as the player's. If not, asks for co-ordinates again.
                    if (p.getColour() != getPieces().getColour()){
                        System.out.println("Please move your own pieces \n");
                        correctMove = false;
                    }

                    //tells player their selected piece
                    System.out.println("Your selected piece is " + p.getChar());

                    //looks through available moves for piece
                    moves = p.availableMoves();

                    //if no moves available, asks for new co-ordinates.
                    if (moves == null){
                        System.out.println("No moves available for this piece \n");
                        correctMove = false;
                    }
                    else {

                        Move found = null;

                        for( Move m : moves){
                            //checks if move can be done
                            if (m.ToX() == toXCo && m.ToY() == toYCo){
                                //if move is allowed- exit loop
                                found = m; 

                                correctMove = true;

                            }
                        }

                        if (found == null) {
                            //if move can't be, ask for new co-ordinates
                            System.out.println("This move is not legal \n");
                            correctMove = false;

                        }   
                    }
                }while(fromXCo != s.getFromXInt() && fromYCo != s.getFromYInt() && toXCo != s.getToXInt() && toYCo != s.getToYInt());
            }

共 (1) 个答案

  1. # 1 楼答案

    如果我正确理解了while的目的,那么如果没有选择带有合法移动的合法物品,用户可以重复选择。你不需要有两个while循环(内部的while看起来就像一个if)。我将这样做(伪代码):

    correctMove = false;
    While(!correctMove) {
        square = inputNewSquare();
        if(isEmpty(square))
            "click on a piece"
        else if(pieceHasRightColor(square))
            "you can only move your own piece"
        else if(!pieceHasMove(square))
            "this piece has no legal move"
        else {
            //move(s) found, do stuff
            correctMove = true;
        }
    }