0

我有几个按钮可以加减。如果计数器下降到 0 或低于表示游戏结束,我希望弹出一个显示。我有 winGame(); 在每个按钮 clicklistener 下设置功能来检查计数器总数。**我遇到的问题是,无论计数器的值是多少,它总是显示文本框说游戏结束。每次单击按钮以添加或减去总自动时都会显示 texbox。另外值得注意的是,当应用程序启动时,计数器从 20 开始。这是代码。

            void winGame() {
    if (counter1 <= 0 || counter <=0 );
    String text = "Game Over";

    // Build a dialog box and with the result string and a single button
    AlertDialog.Builder game = new AlertDialog.Builder(this);
    game.setMessage(text).setCancelable(false)
            .setPositiveButton("OK", new 
            DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id)   
            {
                    // do things when the user clicks ok.
                }
            });
    AlertDialog alert = game.create();

    // Show the dialog box.
    alert.show();
}
4

1 回答 1

4

一个微妙的小错误...

if (counter1 <= 0 || counter <=0 );

应该

if (counter1 <= 0 || counter <=0 )

或者这就是你想要的?

void winGame() {
    if (counter1 <= 0 || counter <=0 )
    {
        String text = "Game Over";

        // Build a dialog box and with the result string and a single button
        AlertDialog.Builder game = new AlertDialog.Builder(this);
        game.setMessage(text).setCancelable(false)
            .setPositiveButton("OK", new 
        DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id)   
            {
                // do things when the user clicks ok.
            }
        });
        AlertDialog alert = game.create();

        // Show the dialog box.
        alert.show();
    }
}
于 2013-11-12T20:10:02.180 回答