Java 我的while循环不';即使布尔值变为false,也无法完成如何修复它?

Java 我的while循环不';即使布尔值变为false,也无法完成如何修复它?,java,while-loop,Java,While Loop,我正在为一个有两个玩家轮流玩的游戏编写代码,但在某些情况下,即使布尔值变为false,while循环也会继续运行 这是有问题的while循环部分 while (stone>0) // this should work only when the num of stones are bigger than 0 { if(turn==2*n-1&&stone>0); //if it's odd num turn, it's player1's turn

我正在为一个有两个玩家轮流玩的游戏编写代码,但在某些情况下,即使布尔值变为false,while循环也会继续运行

这是有问题的while循环部分

while (stone>0)
// this should work only when the num of stones are bigger than 0
{
    if(turn==2*n-1&&stone>0); 
    //if it's odd num turn, it's player1's turn
    {
        System.out.println(p1.getName()+"'s turn - remove how many? : ");
        int remove = keyboard.nextInt();
        s.setStone(remove); 
        stone = stone-s.removeStone();

        System.out.print(stone+"stones left");

        for (i=0;i<stone;i++)
            System.out.print("*");
    }

    //printing asterisk mark to the num of stone left 
    System.out.println(); 

    if(turn==2*n&&stone>0);
    //if it's even num turn, it's player2's turn
    {
        System.out.println(p2.getName()+"'s turn - remove how many? : ");
        int remove = keyboard.nextInt();

        s.setStone(remove);
        stone = stone-s.removeStone();
        System.out.print(stone+"stones left");
        for (i=0;i<stone;i++)
             System.out.print("*");
    } 
    System.out.println();
}
while(stone>0)
//只有当石头的数量大于0时,这才有效
{
如果(旋转==2*n-1&&stone>0);
//如果轮到奇数,则轮到玩家1
{
System.out.println(p1.getName()+“’的回合-删除多少?:”;
int remove=keyboard.nextInt();
s、 设置音调(移除);
石头=石头-s.移除石头();
系统输出打印(石头+“石头左”);
对于(i=0;i0);
//如果是偶数回合,则轮到玩家2
{
System.out.println(p2.getName()+“’的回合-删除多少?:”;
int remove=keyboard.nextInt();
s、 设置音调(移除);
石头=石头-s.移除石头();
系统输出打印(石头+“石头左”);
对于(i=0;i),您可以添加:

if(!(stone > 0))
        break;
在while循环的末尾。 这样,当循环结束时没有石头时,循环应该停止。

你的问题是

if(turn==2*n-1&&stone>0);

if语句为空(意思是:如果条件为true,则不执行任何操作)

这些行末尾的分号是空语句-因此,始终执行以下块(询问每个玩家要移除多少石头)

删除这两个分号,就不需要更改代码的其余部分

if(turn==2*n&&stone>0);