(驾驶费用)编写一个程序,提示用户输人驾驶的距离、以每加仑多少英里的汽车燃油性能,以 及每加仑的价格,然后显示旅程的费用。下面是一个运行示例:
package com.jinling.day01;
import java.util.Scanner;
public class NKTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the driving distance: ");
double distance = sc.nextDouble();
System.out.print("Enter miles per gallon: ");
double milesPerGallon = sc.nextDouble();
System.out.print("Enter price per gallon: ");
double pricePerGallon = sc.nextDouble();
sc.close();
// 使用该方法保留两位小数String.format(format,ags)
System.out.print("The cost of driving is $" + String.format("%.2f", distance / milesPerGallon * pricePerGallon));
}
}