How do I prevent division by zero in my code?

I have one problem with my code, when I choose division it sometimes gives a division by zero error. how do I avoid this? as you can see I tried an if statement to catch that possibility but I still get division by zero errors.

Re: How do I prevent division by zero in my code?

So after you check to see what if something is zero, what does your code do? Specifically, does your code actually change random1 or 2? Remember, variables declared within a method belong to that method - if you call a method again, even inside that method, new variables are made - the old ones aren't overwritten.

Re: How do I prevent division by zero in my code?

Re: How do I prevent division by zero in my code?

I'm so sorry I'm just not good with this stuff I'm trying to understand but I don't. I'll just think loudly for you guys. Below are my thoughts which I'll write as I'm trying to work it out, I usually get things wrong atleast this way you guys might know why I get it wrong.

what does diargg mean? well hes talking about local variables... hes saying that when question is called again new values are created. what would be the values of these? they would be a pass-by-value copy... so question() must get a copy of the current values I'm dealing with.

ok so I'll look at the start of my method since I called it. since difficulty is the same, I must get the same difficulty like last time but then random1+2 should get new values however I think diargg is saying that this copy of random1+2 dont affect the original random1+2 but why does that matter? does program control ever return to the original call of the method? well... lets say my new randoms give me a good result then what happens...

wait a minute I must return back to the original method call! and this will execute random1/random2 with their original values!!! thats it... is it.

yay I might have worked it out!

ok so whats the solution... well I dont want to continue the original cycle I want to go back to the beginning and as I've discovered I cant do that with a method call like that... how do I return program control to the beginning? I would put break afterwards but then it will still check the value of input against result later in the code and that will result in a wrong answer so I cant use break.

I know! GoTo!!! *smack* sorry... (speaking of which does java even have that?) um... agh!I just want to get back to the beginning of the method, why is that so hard...