1

I am struggling to understand why my code will not exit when I tell it to. I want the program to terminate when I tell it to. But in my block of code where I tell it to, it just ignores it then moves onto the next line of code. For example: (here is part of my code):

if (newNum - userNum == 0) {
    System.out.println("You loose!");
    System.exit(0);
    return;
} 

//computer picks
comPick = drawNum();

newNum = newNum - comPick;
System.out.println("The computer picks " + comPick + " number. There are " + newNum + ".");

if (newNum - comPick == 0) {
    System.out.println("You win! Computer Loses!");
    System.exit(0);
    return;
}

When newNum - userNum equals zero it just skips over all of my code and moves onto the next block. Does anyone have any suggestions for me? Thanks in advance!

Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
  • Please provide a short but complete program demonstrating the problem. But making a method terminate the program is *usually* a bad idea, to be honest - it's a better idea for the normal flow of the logic to do that. To put it another way: this code is going to be relatively painful to unit test... – Jon Skeet Feb 15 '14 at 19:02
  • possible duplicate of [When should we call System.exit in Java](http://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java) – aliteralmind Feb 15 '14 at 19:02
  • 3
    wild guess: newNum and userNum are `float` or `double`? :) – Affe Feb 15 '14 at 19:06
  • What @Affe said; also, no need to `return` if you `System.exit()`! – fge Feb 15 '14 at 19:07
  • only with a crystal ball to know – Leo Feb 15 '14 at 19:10

0 Answers0