If the operator logic is not working properly

I am creating an adventure game for text games for my Java class, and my last assignment was to add credibility and training to the game.

Basically, a random number is generated for me, and if the number associated with the Shake skill, for example, is greater than the given number, then the dog will successfully complete the trick.

This part works for me 100%.

Now adding training is where I run into problems. Each skill has an initial value of 1. Each time the skill succeeds, the value increases by 1.

My goal is to have a maximum value of 3, and if the maximum value is reached, then the dog performs the trick every time it is performed!

Here is what I have, hope 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; } } 
+4
source share
3 answers

I can’t say exactly what you are trying to get help with, but the most obvious problem that I see is that in the case when sitSkill == 3 , you still call sitSkill++ , and therefore after this iteration, it will be equal four, causing the else statement to run if the roll is too small.

+7
source

I think you need to stop the sitSkill increment when you reach 3, otherwise it will increase every time the sitSkill == 3 point is always false.

  if(roll >= 4 || sitSkill == 3){ if(sitSkill < 3) { sitSkill++; } System.out.println("\n" + Name + " sat down for you!"); Energy -= 10; Food -= 5; Happy ++; } 
+3
source

change

 if(roll >= 4 || sitSkill == 3) 

to

 if(roll >= 4 || sitSkill >= 3) 

or

 if(Trick.equalsIgnoreCase("Sit")){ if(roll >= 4 || sitSkill == 3){ System.out.println("\n" + Name + " sat down for you!"); Energy -= 10; Food -= 5; sitSkill = sitSkill == 3 ? sitSkill : sitSkill +); Happy ++; } else{ System.out.println("\n" + Name + " did not perform the trick successfuly."); Energy -= 10; Food -= 6; Happy -= 20; } } 
0
source

Source: https://habr.com/ru/post/1499527/


All Articles