下面是一个运算示例:
Enter the driving distance: 900.5
Enter miles per gallon: 25.5
Enter price per gallon: 3.55
The cost of driving is $125.36
package Second;
import java.util.Scanner;
public class Code_23 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the driving distance:");
double distance = input.nextDouble();
System.out.print("Enter miles per gallon:");
double gallon = input.nextDouble();
System.out.print("Enter price per gallon:");
double price = input.nextDouble();
double sum = distance / gallon * price;
System.out.println("The cost of driving is $:" + String.format("%.2f", sum));
}
}
输出
Enter the driving distance:900.5 Enter miles per gallon:25.5 Enter price per gallon:3.55 The cost of driving is $:125.36



