正确结束计划

时间:2014-12-15 15:25:25

标签: java

我有一个游戏,ConnectFour播放,什么不播放。但是当用户完成游戏时,会出现一个提示:“你想再玩一次吗?如果用户输入是,那么它可以正常工作。但是如果用户输入”No“,则表示打印,”感谢您的播放。“但是终端窗口中的输出是:

  恭喜红色赢得比赛。

     你想再玩一次吗? Y / N

     

名词

     

感谢您的参与。

它不会重新加载可用于将内容输入终端的线路。这一行

Jacks-MacBook-Pro:Cs124 Jack_XXX$ 

我不知道这是否是使用GUI的主程序的功能以及它不会如何关闭。但我必须每次都以这种方式结束控制C,我想知道是否有办法阻止它。

main的代码是:

public static void main(String[] args) {

ConnectFour connectFour= new ConnectFour();
BoardView view =  connectFour.getView();

JFrame worldFrame = new JFrame();
JPanel buttonPanel = new JPanel();


view.repaint();

worldFrame.setContentPane(view);
worldFrame.add(buttonPanel);
worldFrame.validate();
worldFrame.pack();
worldFrame.setVisible(true);


Scanner input = new Scanner(System.in);
System.out.println("Welcome to Connect Four. The object of the game is to get four of the same colored pieces in a row.");
System.out.println("Red will go first.");

//sets up the gameboard and then draws it
for (int i = 0; i < connectFour.getGameBoard().length; i++) {
  for (int j = 0; j < connectFour.getGameBoard()[0].length; j++) {
    connectFour.getGameBoard()[i][j] = 0;
  }
}

view.repaint();

connectFour.printGameBoard();

String pwon = "";
boolean playgame=true;
//actual gamecode
while(playgame=true){
    boolean game=false;
    while (game == false) {

        System.out.println("Red, which column would you like to move into?");
        System.out.println("Note Column Numbers 1 to 7 are accepted.");

        int x = input.nextInt();
        connectFour.move1(x, connectFour.getGameBoard());
        if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
            System.out.println("Invalid move. You lost your turn");
        }
        view.repaint();
        connectFour.printGameBoard();

        if (connectFour.isFourInaRow()) {
            pwon = "Red";
            break;
        }

        System.out.println("Black, which column would you like to move into?");
        System.out.println("Note Column Numbers 1 to 7 are accepted.");

        x = input.nextInt();
        connectFour.move2(x, connectFour.getGameBoard());
        if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
            System.out.println("Invalid move. You lost your turn");
        }
        view.repaint();
        connectFour.printGameBoard();

        if (connectFour.isFourInaRow()) {
            pwon = "Black";
            break;
        }

        }

    if (connectFour.checkRow() == true) {
        System.out.println("row");
    } else if (connectFour.checkColumn() == true) {
        System.out.println("col");
    } else if (connectFour.checkDiag() == true) {
        System.out.println("diag");
    } else{ 
        System.out.println("draw");

    }

    System.out.println("Congrats " + pwon + " won the game.");
    System.out.println("Would you like to play again? Y/N");
    input.nextLine();
    String answer = input.nextLine();

    if((answer.equals("N"))||(answer.equals("n"))){
        break;
    }
 }
 System.out.println("Thanks for playing.");


 }

4 个答案:

答案 0 :(得分:3)

创建JFrame时,程序会启动一个运行消息循环的新线程。在此循环运行时它不会终止。

最简单的方法就是致电System.exit(0)

答案 1 :(得分:1)

两个input.nextLine()?

input.nextLine();
String answer = input.nextLine();

你只写

String answer = input.nextLine();

答案 2 :(得分:1)

试试这个:

if ( answer . toUpperCase ( ) . equals ( "N" ) ) 
{
    System . out . println ( "Thanks for playing!" ) ; 
    System . exit ( 0 ) ; 
}

答案 3 :(得分:0)

您可以使用System.exit();退出该应用程序。此外,您应在Jframe上设置默认关闭操作。像worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

这样的东西