开发者社区> 问答> 正文

为什么不重复三遍?

我不知道这会不断重复“最后一个迷路的人”。我想尝试使该游戏重复3次,以便玩家可以重复3次,但不会重复。另外,当我尝试输入if(enter!= 1 ||| enter!= 2)时,即使我输入1或2也会不断重复自我,有人可以帮我吗?

for(d=0;d<3;d++)
    {
    while(sticks>0) {
    System.out.println("Player 1 how many sticks would you like to take? 1 or 2?");
    int  enter = numScan.nextInt();//this tells us how many they're going to take
    sticks=sticks-enter;
    if(enter!=1||enter!=2)//this makes it so people who don't enter a right number are forced to replay the program and suffer
    {
        System.out.println("please run this program again");

    }
    System.out.println("There are "+sticks+" sticks left!");
    System.out.println("Player 2 how many sticks would you like to take? 1 or 2");
    System.out.println("Ther are "+sticks+" sticks left!");
    int enter2=numScan.nextInt();//this is the second enter 
     sticks=sticks-enter2;//this will tell us how many sticks are left 
    if(enter2!=1 || enter2!=2)//this makes it so people who don't enter a right number are forced to replay the program and suffer
    {
        System.out.println("please run this program again");

    }

    System.out.println("Player 1 how many sticks would you like to take? 1 or 2?");
    int enter3=numScan.nextInt();//this is the second enter 
    sticks=sticks-enter3;//this will tell us how many sticks are left 
    System.out.println("There are "+sticks+" sticks left!");
    }
    if(sticks<0||sticks==0)
    {
        System.out.println("The last player who went lost!");
    }
}





}

}

展开
收起
垚tutu 2019-12-19 16:47:13 716 0
1 条回答
写回答
取消 提交回答
  • #include

    如评论中所述,在第一个游戏结束后仍然收到相同消息的原因sticks是,在游戏结束(或开始)时未重置的值。

    为了解决您的问题,我建议您sticks在游戏开始前设置的值,如下所示:

    for(d=0;d<3;d++)
        sticks = 20; //the value with which you initialize the game
        {
        while(sticks>0) {
        System.out.println("Player 1 how many sticks would you like to take? 1 or 2?");
        int  enter = numScan.nextInt();//this tells us how many they're going to take
    
    

    可能还有其他解决方案。但通常,您应该在开始时设置游戏的“规则”或“初始化变量”。

    2019-12-19 16:47:26
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载