I have a problem with some Java code

1    24 Oct 2018 00:10 by u/seidoken

I'm taking a introduction to Java class and one of the conditions for an else if statement fails occasionally. Its supposed to check if all three numbers are equal and return a 5 but sometimes it will return the 5 if the numbers are not equal. I don't think there is anything wrong with my logic, can anyone tell me what I'm doing wrong?

else if ( x == y && y == z && x == z) { point1 = point1 + 5; }

34 comments

0

For value checks generally use equals (). The == is for reference comparison. It depends upon, static, objects, primitive types, etc. Also you're adding five to point1, not returning 5 in this snippet.

0

They are simple integers.

0

Java has a bias towards over using objects. :/

0

if x == y and y == z then x will be == to z, I think that's redundant. I don't think that would effect the problem however. I would setup a System.out.println() to see what the values are so you can determine why the logic is failing.

0

The x == z comparison is unnecessary. Also, why are you adding 5 to the existing value of point1, instead of just assigning it directly?

point1 might be already equal to 5 when you enter this condition. Use your debugger to see what's going on, or if your want to do it ghetto style, add a print statement that shows the values of x, y, and z whenever the code block under the if statement is entered.

0

Many games have chance bonuses, like snake eyes. Just assumed it was the rules of his game...

0

I don't see anything wrong with it. I'd leave out the "&& x == z" as that's redundant, but it shouldn't cause any problems.

Some thoughts:

  • Are x, y, and z all ints? This would all be very wrong if they were objects instead of primitives.
  • Could something be going on in the earlier part of the if statement?
  • You say "return a 5." I assume you mean "add 5 to point1" since this is obviously not returning anything. Could the value be added somewhere else in the code?
0

They are all declared ints, this is the only position that adds a 5 to the value, other positions either add 1 or 21.

0

Are you wishing to compare values or references?

Also, I'm thinking associative property can be more efficient (sorry to sound like a stickler)

0

I'm comparing the values of integers.

0

does it have to be one IF statement?

0

The only way I can see this failing is if the variables are Objects, rather than primitive types. I.e. if they are Integer, rather than int.

If they are Objects, then use:

else if(x.equals(y) && x.equals(z)) { point1 = point1 + 5; }

Like others have said, you can omit the third comparison as it is redundant. Obviously here you will have to be sure that x is not null too, or that if it is null, that y and z are also null.

There is also a possibility that the problem is in the preceding if statement, since this is an else/if. Hard to say without seeing more of the code though.

0

x, y, and z are all integers I added the x==z because it was failing occasionally.

0

If your description is accurate, it fails when point1 =5 before the statement starts as you're adding to point1 when it matches the conditional, and if youre expecting 5, the conditional only works when point1 starts as 0.

And you can get rid of x==z, as many have said.

0

(((Java)))

0

i recommend you to ask help with https://www.assignmentspot.co.uk/

0

This is the code preceding the else if, I don't see how it would effect i

  for (int counter=1; counter>0;){
     die1.roll();
     die2.roll();
     die3.roll();
     int x = die1.getFaceValue();
     int y = die2.getFaceValue();
     int z = die3.getFaceValue();
     if (x == round6 && y == round6 && z== round6)
     {
        point6 = point6 + 21;
        counter--;
     }
0

Post more of the code, please.

0

You need a closing brace for both the for loop and the if statement inside it. I would recommend using a while instead of for.

I don't see what your try-again condition is but you could do something like this.

boolean done = false;
while(!done) {
}

Breaking out of the loop with a break statement when finished with rolling can be reasonable too.

0

I'll post the complete code.

0

You need to post the whole code, including the return statement. Ideally as a self-contained test case. Just because you think the bug is in a particular line doesn't mean it is.

0

did you resolve the problem?

0

I'm stuck dealing with work stuff at the moment, some of my co-workers are computer science majors and I'll run it by them once we get settled down. I'll post whatever we find out.

0

If you want something fixed properly, you want a software engineer, not students.

0

You could have used (x == y && x == z) as a condition, assuming primitive int-'s. The three-way expression resembles a swap idiom, but it is good to be explicit in the business logic however conceived.

I don't see what the previous (or later) conditionals in the if-else... chain are, execution probably branched to one of the other conditionals (i.e. (x == 5) taking a higher priority than than (x == y && x == z))

0

This is the complete code for the program, its a dice game called bunco where you gain points by dice rolls matching the round number, throwing three matching numbers, and throwing three matching numbers that match the round number. I added the extra x== z because the match 3 will occasionally give a + 5 even without anything matching but it still continues to do that.

Here is a link to the full code.

0

I suck at Java, and programming in general but got this going. https://justpaste.it/5ygg4

0

Thanks for the help, but I'm still curious as to why my code was failing.

0

The code as written in your paste doesn't import java.util.Random or anything else that might provide a dice class... It won't compile because of that.

Maybe you used something like this? http://quabr.com/29466475/java-dice-program I should try that...

Okay that works. Your program as written works using the Die.java code from that link.


I got your program to work and eventually understood the rules of the game, my solution was wrong. Sorry about that.


I tried to rewrite a solution following the rules properly but neither this nor your version will compile. It errors on import Die.

That was a hiccup solved in a couple minutes the first time but a roadblock now. Maybe I'm too tired? I don't know why it won't compile now...


I misunderstood your rules. I should rewrite this... What was your question? Where the extra 5 points comes from. Let me try more test runs...

I see, it doesn't happen very often. The problem is you don't see the mini-BUNCO rolls, only that it was a mini-BUNCO. The 5 extra points come from the mini-BUNCO. You wouldn't see those because of not printing the roll in those cases but the score was still added.


Your problem seems to be mixing the logic for printing a roll, calculating the score, and figuring whether to roll again. Those should be totally seperate blocks of code.

Sorry, I would demonstrate but the Die class won't import anymore. I can't get your old version nor my new updated version to work now. :(

0

Thanks for the advice I'll keep that in mind for future projects. The assignment was to practice calling premade classes I didn't think to include the die.java when I posted my code. All the randomization happens there when the roll method is called.

0

Oh. They probably want you to create jar files. I don't have jar installed on this machine... Drat, I ought to do that too haha!

0

I am not a java expert

  • Verify how getFaceValue(); is implemented. I am wondering if you return an int in here or are casting an object to an int value in this part: "int x = die1.getFaceValue();"
  • There are 6 different "int x, y, z" that have the same name.
  • You probably used copy&paste. I checked if you did not have typo's. Java works JIT I assume could an invisible character break the logic?
0

I think you should get to help an expert developer in Mujadidia.

0