I have been trying this for hours and cant\' get it right? what am i missing? I
ID: 669926 • Letter: I
Question
I have been trying this for hours and cant' get it right? what am i missing? I am trying to get the JAVA program to round up to the nearest integer......I am using jgrasp and every time i get an error in the complier???
public class RounderProject
{
Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
long answer;
double input;
System.out.println("Enter any decimal number. It will be rounded to the nearest integer.");
input = console.nextDouble();
System.out.println("You entered" + " " + input);
answer = round(input);
System.out.println("The answer is" + " " + answer);
Explanation / Answer
/**The java program RounderProject that prompts user to enter the decimal value and prints the nearest integer value of decimal number. Then prints the value on console*/
//RounderProject.java
import java.util.Scanner;
public class RounderProject
{
public static void main(String[] args)
{
//Create an object,console inside main method
Scanner console = new Scanner(System.in);
long answer;
double input;
//prompt user to enter a decimal number
System.out.println("Enter any decimal number. " +
"It will be rounded to the nearest integer.");
//read double value from keyboard
input = console.nextDouble();
System.out.println("You entered" + " " + input);
//Call the method round of Math class to get nearest integer value.
answer = Math.round(input);
//print answer
System.out.println("The answer is" + " " + answer);
}//end of the main method
} //end of the class RounderProject
------------------------------------------------------------------------------------------
Sample Output:
Enter any decimal number. It will be rounded to the nearest integer.
4.56
You entered 4.56
The answer is 5
------------------------------------------------------------------------------------------
Note : If you want to create an object of Scanner class outside main method,
write static keyword before Scanner class to use inside a static
method main .
static Scanner console = new Scanner(System.in);
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.