Problem:
Write an application that converts miles to kilometers. (One mile
equals 1.60935 kilometers.) Read the miles value from the user
as a floating point value.
equals 1.60935 kilometers.) Read the miles value from the user
as a floating point value.
Output:
Enter the distance in miles: 423
That distance in kilometers is: 680.75505
That distance in kilometers is: 680.75505
Solution:
import java.util.Scanner; public class MilesToKilometers { //----------------------------------------------------------------- // Converts miles into kilometers. The value for miles is read // from the user. //----------------------------------------------------------------- public static void main (String[] args) { final double MILES_PER_KILOMETER = 1.60935; double miles, kilometers; Scanner scan = new Scanner(System.in); System.out.print ("Enter the distance in miles: "); miles = scan.nextDouble(); kilometers = MILES_PER_KILOMETER * miles; System.out.println ("That distance in kilometers is: " + kilometers); } }
No comments :
Post a Comment