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)
JavaScript
x
9
1
function isNearZero(num) {
2
const threshold = 0.001;
3
return num < threshold && num > -threshold;
4
}
5
6
while (!isNearZero(value = input.nextDouble())) {
7
// do things
8
}
9