Java Reference
In-Depth Information
LISTING 2.8
continued
{
String message;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter a line of text:");
message = scan.nextLine();
System.out.println ("You entered: \"" + message + "\"");
}
}
OUTPUT
Enter a line of text:
Set your laser printer on stun!
You entered: "Set your laser printer on stun!"
As you can see by the output of the GasMileage program, the calculation pro-
duces a floating point result that is accurate to several decimal places. In the next
chapter we discuss classes that help us format our output in various ways, includ-
ing rounding a floating point value to a particular number of decimal places.
LISTING 2.9
//********************************************************************
// GasMileage.java Author: Lewis/Loftus
//
// Demonstrates the use of the Scanner class to read numeric data.
//********************************************************************
import java.util.Scanner;
public class GasMileage
{
//-----------------------------------------------------------------
// Calculates fuel efficiency based on values entered by the
// user.
//-----------------------------------------------------------------
public static void main (String[] args)
 
Search WWH ::




Custom Search