Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading