I was just wondering if you guys could help me out with a problem?.
double num = 2.0
if(num%1 != 0)
{
System.out.println("hi");
}
else if(num%.5 != 0)
{
System.out.println("hi there");
}
basically i want to make sure a number the user enters ends with .0 or .5.....but i am having trouble figuring it out. The above "solution" is what i came up with but it doesnt seem to work. Any pointers?, im relatively new to programming by the way so any help would be greatly apprecaited.
The %
operator works on the floating-point types float
and double
as well as the integral types in Java. Any number that ends in ".0" or ".5" is divisible by 0.5
.
if (num % 0.5 == 0)
{
System.out.println("Ends in .0 or .5!");
}
try this
if (num - Math.rint(num) == 0) {
System.out.println("hi");
} else if (Math.abs(num - Math.rint(num)) == 0.5) {
System.out.println("hi there");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.