我一直在线程“ main” java.util.NoSuchElementException错误消息中获取异常。我已经尝试过进行切换,但是仍然遇到这个问题
我尝试用不同的方法声明变量,但是似乎没有任何效果。
import java.util.Scanner;
public class LabProgram {
public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) {
double totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon;
return totalCost;
}
public static void main(String[] args) {
double milesG;
double dollarsG;
Scanner scnr = new Scanner(System.in);
milesG = scnr.nextDouble();
dollarsG = scnr.nextDouble();
drivingCost(10.0, milesG, dollarsG);
milesG = scnr.nextDouble();
dollarsG = scnr.nextDouble();
drivingCost(50.0, milesG, dollarsG);
milesG = scnr.nextDouble();
dollarsG = scnr.nextDouble();
drivingCost(400.0, milesG, dollarsG);
}
}
问题是:
使用输入参数DriverMiles,milesPerGallon和dollarsPerGallon编写一个方法drivingCost(),该方法返回驱动这些英里的美元成本。所有项目均为double类型。如果使用50 20.0 3.1599调用该方法,则该方法返回7.89975。
在一个程序中定义该方法,该程序的输入为汽车的英里/加仑和汽油美元/加仑(均加倍)。通过三次调用DrivingCost()方法,输出10英里,50英里和400英里的汽油成本。 输出每个浮点数,小数点后两位。
输入是:20.0 3.1599
预期输出:1.58 7.90 63.20
答案 0 :(得分:0)
您在主函数中调用了scnr.nextDouble();
六次。运行程序时,请确保提供六个类型为 double 的参数。当前,您传递的参数少于六个,并且scnr.nextDouble();
引发异常,因为找不到下一个类型为double的参数。
答案 1 :(得分:0)
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);
}
}