我不知道这会不断重复“最后一个迷路的人”。我想尝试使该游戏重复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!");
}
}
}
}
如评论中所述,在第一个游戏结束后仍然收到相同消息的原因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
可能还有其他解决方案。但通常,您应该在开始时设置游戏的“规则”或“初始化变量”。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。