我不知道这会不断重复“最后一个迷路的人”。我想尝试使该游戏重复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!");
}
}
}
}
答案 0 :(得分:1)
正如comment中所提到的,您在第一局游戏结束后仍然收到相同消息的原因是,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
可能还有其他解决方案。但是通常,您应该在开始时设置游戏的“规则”或“初始化变量”。
答案 1 :(得分:0)
if(enter!=1||enter!=2)
更改为
if(enter!=1 && enter!=2) //not easy to understand and requires thinking
if(sticks<0||sticks==0) //equivalent to if(sticks<=0)