Home > database >  How to terminate program if value of 0 or anything over .001 is inputted
How to terminate program if value of 0 or anything over .001 is inputted

Time:10-16

So I know using a while loop like this while ((value = input.nextDouble()) != 0) makes it so when 0 is inputted the program terminates and the print statements are printed, but how would I make it so that if anything less than .001 or 0 makes the program terminate?

CodePudding user response:

Here is a quick function for checking if a number is within a threshold of 0:

(this differs from my previous comment in that it will check if the value is within the threshold, but negative)

function isNearZero(num) {
  const threshold = 0.001;
  return num < threshold && num > -threshold;
}

while (!isNearZero(value = input.nextDouble())) {
  // do things
}
  • Related