Skip to content
Advertisement

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

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?

Advertisement

Answer

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
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement