I am tasked with creating a program that rounds a number such as 2.7 to 3 and 2.5 to 2. I have already written a code that does this but my teacher wants the output in int. (i.e. 2.7 = 3). Here is what I have:
import java.util.*; import java.util.Scanner; public class HandsOn14JamesVincent { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter a decimal number and I will round it."); float num = input.nextFloat(); float num1 = Math.round(num); float num2 = num1 - num; if (num2 <= .3) System.out.println(Math.ceil(num)); if (num2 > .3) System.out.println(Math.floor(num)); } } It displays the correct number, but it displays them in float (2.0). I can't seem to figure out how to convert them. I tried multiplying the num by (int) but it didn't work. Please Help!!
(int) floatingPointNumber(int)numis not called multiplying with(int), it is called casting toint.