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 u/cantaloupe6 24 Oct 2018 00:27
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 u/seidoken [OP] 24 Oct 2018 10:13
They are simple integers.
0 u/hfkmfn 26 Oct 2018 13:49
Java has a bias towards over using objects. :/
0 u/eongoat 24 Oct 2018 00:27
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 u/Kromulent 24 Oct 2018 00:27
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 u/hfkmfn 26 Oct 2018 13:46
Many games have chance bonuses, like snake eyes. Just assumed it was the rules of his game...
0 u/NassTee 24 Oct 2018 00:28
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:
0 u/seidoken [OP] 24 Oct 2018 10:29
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 u/SukkhaMadiqqa 24 Oct 2018 00:33
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 u/seidoken [OP] 24 Oct 2018 10:14
I'm comparing the values of integers.
0 u/RustyEquipment 24 Oct 2018 00:43
does it have to be one IF statement?
0 u/Gumbatron 24 Oct 2018 00:51
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 u/seidoken [OP] 24 Oct 2018 10:15
x, y, and z are all integers I added the x==z because it was failing occasionally.
0 u/avgwhtguy1 24 Oct 2018 01:24
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 u/TurdLord5000 24 Oct 2018 02:00
(((Java)))
0 u/mandiken 24 Oct 2018 06:35
i recommend you to ask help with https://www.assignmentspot.co.uk/
0 u/seidoken [OP] 24 Oct 2018 10:29
This is the code preceding the else if, I don't see how it would effect i
0 u/aCuriousYahnz 24 Oct 2018 18:37
Post more of the code, please.
0 u/hfkmfn 26 Oct 2018 13:41
You need a closing brace for both the
forloop and theifstatement inside it. I would recommend using awhileinstead offor.I don't see what your try-again condition is but you could do something like this.
Breaking out of the loop with a
breakstatement when finished with rolling can be reasonable too.0 u/seidoken [OP] 26 Oct 2018 22:17
I'll post the complete code.
0 u/KikesDidJFK 02 Nov 2018 06:41
You need to post the whole code, including the
returnstatement. Ideally as a self-contained test case. Just because you think the bug is in a particular line doesn't mean it is.0 u/eongoat 25 Oct 2018 23:39
did you resolve the problem?
0 u/seidoken [OP] 26 Oct 2018 00:05
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 u/KikesDidJFK 02 Nov 2018 06:43
If you want something fixed properly, you want a software engineer, not students.
0 u/hfkmfn 26 Oct 2018 13:28
You could have used
(x == y && x == z)as a condition, assuming primitiveint-'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 u/seidoken [OP] 26 Oct 2018 22:30
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 u/hfkmfn 27 Oct 2018 01:07
I suck at Java, and programming in general but got this going. https://justpaste.it/5ygg4
0 u/seidoken [OP] 29 Oct 2018 03:09
Thanks for the help, but I'm still curious as to why my code was failing.
0 u/hfkmfn 29 Oct 2018 07:26
The code as written in your paste doesn't import
java.util.Randomor 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.javacode 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
Dieclass won't import anymore. I can't get your old version nor my new updated version to work now. :(0 u/seidoken [OP] 29 Oct 2018 13:04
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 u/hfkmfn 29 Oct 2018 14:59
Oh. They probably want you to create jar files. I don't have
jarinstalled on this machine... Drat, I ought to do that too haha!0 u/roznak 27 Oct 2018 01:56
I am not a java expert
0 u/Erinrookes 31 Oct 2018 07:10
I think you should get to help an expert developer in Mujadidia.
0 u/Erinrookes 27 Dec 2018 13:20
I think this post helps you out https://www.mujadidia.com/java-programming-errors-that-must-be-sorted/.