4 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
import java.util.Scanner;
public class LabProgram {
/* Define your method here */
public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) {
double totalCost = (dollarsPerGallon * drivenMiles / milesPerGallon);
return totalCost;
}
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
double milesPerGallon = scnr.nextDouble();
double dollarsPerGallon = scnr.nextDouble();
double drivenMiles = 1;
System.out.printf("%.2f ", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 10);
System.out.printf("%.2f ", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 50);
System.out.printf("%.2f\n", drivingCost(drivenMiles, milesPerGallon, dollarsPerGallon) * 400);
}
}

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
driveMiles 除以每加侖英里數(shù),然后乘以每加侖美元,即可得出每英里行駛的汽油價(jià)格。注意:在這種情況下,drivenMiles 只需要傳遞給 movingCost。這就是為什么要添加整數(shù) 10、50 和 400 來(lái)調(diào)用。
由于 DrivingCost 按此順序具有milesPerGallon、dollarsPerGallon 和drivenMiles 參數(shù),因此您必須使用相同的參數(shù)順序調(diào)用該方法。
“%.2f”將得到右邊兩位小數(shù)。添加 \n 后將另起一行。
import java.util.Scanner;
public class LabProgram {
public static double drivingCost(double milesPerGallon, double dollarsPerGallon, double drivenMiles) {
// calcuating the cost of gas
double totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon;
return totalCost;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double milesPerGallon;
double dollarsPerGallon;
milesPerGallon = scnr.nextDouble();
dollarsPerGallon = scnr.nextDouble();
// order of the call to the method is important, printing cost of gas for 10, 50, and 400 miles
System.out.printf("%.2f ",drivingCost(milesPerGallon, dollarsPerGallon, 10));
System.out.printf("%.2f ",drivingCost(milesPerGallon, dollarsPerGallon, 50));
System.out.printf("%.2f\n",drivingCost(milesPerGallon, dollarsPerGallon, 400));
}
}

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
import java.util.Scanner;
public class LabProgram {
public static double drivingCost(double milesPerGallon, double dollarsPerGallon, double drivenMiles) {
return (drivenMiles / milesPerGallon) * dollarsPerGallon;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double milesPerGallon, dollarsPerGallon;
milesPerGallon = input.nextDouble();
dollarsPerGallon = input.nextDouble();
System.out.printf("%.2f ", drivingCost(milesPerGallon, dollarsPerGallon, 10));
System.out.printf("%.2f ", drivingCost(milesPerGallon, dollarsPerGallon, 50));
System.out.printf("%.2f\n", drivingCost(milesPerGallon, dollarsPerGallon, 400));
}
}

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
您在主函數(shù)中調(diào)用了scnr.nextDouble();
六次。確保在運(yùn)行程序時(shí)提供六個(gè)double類(lèi)型的參數(shù)。目前,您傳遞的參數(shù)少于六個(gè),并且scnr.nextDouble();
拋出異常,因?yàn)樗也坏较乱粋€(gè) double 類(lèi)型的參數(shù)。
添加回答
舉報(bào)