0

I don't know this keeps repeating "The last person who went lost". I want to try making this repeat the game 3 times so players can plat it 3 times but it won't repeat. Also, when I try my if(enter!=1||enter!=2) stuff keeps repeating its self even when I type in 1 or 2 can someone help me please?

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!");
    }
}





}

}

  • 2
    You never reset the value of `sticks`. So once it is zero (or less), it is **always** zero (or less). Also every number will match `enter!=1||enter!=2` because one is not two **and** two is not one. – Elliott Frisch Dec 18 '19 at 23:15
  • Can you show any value for which `if(enter2!=1 || enter2!=2)` would be false? Please "calculate" it on paper. Related: [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/q/26337003) – Pshemo Dec 18 '19 at 23:17
  • How do I reset the value of sticks? – Finn Uchiha Dec 18 '19 at 23:23

2 Answers2

1

As was mentioned in the comment, the reason why you keep getting the same message after the first game finishes is the fact that the value of sticks is not being reset at the end (or the start) of the game.

In order to solve your problem I would suggest setting the value of sticks before the round of the game starts, like this:

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

There might be other solutions. But in general, you should set the "rules" of the game or "initialize the variables" in the beginning.

Timothy
  • 50
  • 1
  • 1
  • 10
0
if(enter!=1||enter!=2)

change to

if(enter!=1 && enter!=2) //not easy to understand and requires thinking

if(sticks<0||sticks==0) //equivalent to if(sticks<=0)
charlesreid1
  • 4,360
  • 4
  • 30
  • 52