+ 4
I need some help debugging this Java code please.
I thought it was working perfectly, but I just realized that is not the case. Bug: If player B wins the match, the declaration of the winner is not printed. for instance, if you input 1 as player A and 7 as Player B (Cloud vs Defaultius Maximus) Cloud will lose the fight, but it will not declare Defaultius as the winner, the code execution just stops. https://code.sololearn.com/c22FNtmaFPkQ/?ref=app
25 Answers
+ 4
Heres the code :-(its not public, so u dont need to worry xD)
https://code.sololearn.com/cZD8s3xiD1MP/?ref=app
+ 5
If user enters any integer except for 1-6, the pick will default to Defaultius Maximus. I will work on adding a way to handle a NaN input. Thanks for the tip!
+ 5
Actually, I already took care of the type validation issue. @line 152 in the repaired code you provided.
+ 5
Yes!! I didn't notice.
+ 5
Your if, if blocks(last 2 ones) should be out of the while loop!!. If they are inside, the break statement indirectly affects them as well!!
+ 5
Its OK :-)
+ 4
The problem seems to be within this block of code, which is nested inside of a Do..While loop. The second If statement never executes.
if (bHp <= 0){ //If second fighter health drops to <= zero.
System.out.println(a.choiceToString() + " has defeated " + b.choiceToString() + " in battle." + "\n" + a.choiceToString() + " is the WINNER!");
continue;
}
if (aHp <= 0){ //If first fighter health drops to <= zero.
System.out.println(b.choiceToString() + " has defeated " + a.choiceToString() + " in battle." + "\n" + b.choiceToString() + " is the WINNER!");
continue;
}
I tried turning it into an If...Else statement, but I get the same result, the second statement does not execute.
+ 4
Move if blocks at the the last, after line 216
+ 4
I re-repaired it, chk out from above link.
Also, you can replace while with do while, it doesnt matter
+ 4
:-) My pleasure
Anytime :-)
+ 4
Anytime :-)
+ 4
So, I updated my code and now I'm getting an error about my scanner object...I didn't change anything regarding my scanner -_-
https://code.sololearn.com/c2BxG805Ub2v/?ref=app
+ 4
You don not need 2nd condition in while(!sc...)
Actually the second condition sc.next() moves the Scanner object to the next line. So it gives Scanner error. If you let the code as it is and give any input in first line followed by the 2 required inputs (total 3 inputs) it will work.
+ 4
@Meharban Thanks a bunch, I see what you mean now
+ 4
Or remove sc.next() == ""
+ 4
Anytime :-) @Mark Paramonte
+ 4
Yes
+ 4
I just found that with first input 3, second input 1, I get the issue where winner is not declared again :(
+ 4
fixed. I'm sorry for not catching that. I see what you mean.
+ 3
Thank you. That DOES fix the problem I'm having, BUT it also causes a bug where the dead fighter attacks 1 more time after its health reaches <=0