Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement logic not working as expected

Tags:

java

loops

Im making a dog text adventure game for my Java class, and my latest assignment was to add probability and training to the game.

Basically I have a random number generated, and if the number associated with the skill "Shake " for instance is greater then a set number, then the dog will perform the trick successfully.

That part is working 100% for me.

Now adding training is where Im running into problems. I have each skill set to an initial value of 1. Each time a skill is performed successfully the value increases by 1.

My goal is to have the max value of 3, and if the max valued is reached, then the dog performs the trick every single time its executed!

Here is what I have, hopefully someone can explain why its not working

    // Sit
    if (Trick.equalsIgnoreCase("Sit")) {
        if (roll >= 4 || sitSkill == 3) {
            System.out.println("\n" + name + " sat down for you!");
            energy -= 10;
            food -= 5;
            sitSkill ++;
            happy ++;
        } else {
            System.out.println("\n" + name + " did not perform the trick successfuly.");
            energy -= 10;
            food -= 6;
            happy -= 20;
        }
    }
like image 914
23k Avatar asked Dec 25 '22 21:12

23k


2 Answers

I can't tell exactly what you're trying to get help with, but the most obvious issue that I can see is that in the case where sitSkill == 3, you still call sitSkill++, and so after that iteration, it will be equal to four, causing the else statement to be triggered if the roll is too low.

like image 161
qaphla Avatar answered Jan 15 '23 05:01

qaphla


I think you have to stop the increment sitSkill once you reach 3, otherwise it will increment every time at which pointsitSkill == 3 would always be false.

 if(roll >= 4 || sitSkill == 3){
    if(sitSkill < 3) {
        sitSkill++;
    }
    System.out.println("\n" + Name + " sat down for you!");
    Energy -= 10;
    Food -= 5;
    Happy ++;
}
like image 27
Dimitar Dimitrov Avatar answered Jan 15 '23 07:01

Dimitar Dimitrov