As part of my homework, actually this part is for extra credit. I am supposed to write a program that prompts the user to input a decimal number and outputs the number to the nearest integer. Using only things I have learned to this point. I couldn't figure it out. I read ahead in the book about if else statements and came up with the following, but is there a way to do it with out if, else statements?(I am only 1 week into this class, so I can't use anything like the Math round method. I am supposed to do this programmatically):
//Chapter 2 programming exercises 6.) import java.util.*; public class Ch2_PrExercises6 { public static void main(String[] args) { Scanner tv = new Scanner(System.in); int numResult = 0; double numEntered; System.out.println("Enter in a decimal number to be rounded to the nearest whole number, then press Enter: "); System.out.println(); numEntered = tv.nextDouble(); if(numEntered % 1 >= (1/2)) { numResult = (int)(numEntered) + 1; } else { numResult = (int)(numEntered); } System.out.println("You number rounded to the nearest whole number = " + numResult); } }
thank you in advance!