如何检查do-while循环中是否按下了一个键[在Java中]

时间:2017-10-22 12:20:21

标签: java key do-while interrupted-exception keyboardinterrupt

这是一个可以“掷骰子”的游戏

我希望数字1-6在控制台中被旋转,直到玩家按下一个键。按下时显示的数字是要选择的数字。

谢谢!

我的代码看起来像这样:

do {


    System.out.print("\b1");
        Thread.sleep(100);

    if (System.in != null) {
    dice1Player = 1;
    break;

    }



    System.out.print("\b2");
    Thread.sleep(100);

    if (System.in != null) {

    dice1Player = 2;
    break;
}


    System.out.print("\b3");
    Thread.sleep(100);

    if (System.in != null){

    dice1Player = 3;
    break;
    }


    System.out.print("\b4");
    Thread.sleep(100);
    if (System.in != null){

    dice1Player = 4;
    break;
    }

    System.out.print("\b5");
    Thread.sleep(100);
    if (System.in != null){

    dice1Player = 5;
    break;
    }


    System.out.print("\b6");
    Thread.sleep(100);
    if  (System.in != null){

    dice1Player = 6;
    break;
    }
    Thread.sleep(100);

} while (!Thread.currentThread().isInterrupted());

0 个答案:

没有答案