2 线程“main”中的方法异常 java.util.NoSuchElementException

我不断收到 Exception in thread "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);

    }

}

问题是:


使用输入参数drivenMiles、milesPerGallon 和dollarsPerGallon 编写一个driveCost() 方法,该方法返回驾驶这些里程的美元成本。所有项目都是 double 类型。如果使用 50 20.0 3.1599 调用该方法,则该方法返回 7.89975。


在程序中定义该方法,其输入是汽车的英里/加仑和汽油美元/加仑(均为双倍)。通过调用 DrivingCost() 方法 3 次,输出 10 英里、50 英里和 400 英里的汽油成本。输出每个浮点值,小数点后两位。


输入为:20.0 3.1599


预期产量:1.58 7.90 63.20


红颜莎娜
浏览 149回答 4
4回答

阿波罗的战车

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);   }}

大话西游666

driveMiles 除以每加仑英里数,然后乘以每加仑美元,即可得出每英里行驶的汽油价格。注意:在这种情况下,drivenMiles 只需要传递给 movingCost。这就是为什么要添加整数 10、50 和 400 来调用。由于 DrivingCost 按此顺序具有milesPerGallon、dollarsPerGallon 和drivenMiles 参数,因此您必须使用相同的参数顺序调用该方法。“%.2f”将得到右边两位小数。添加 \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));   }}

森林海

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));      }}

蛊毒传说

您在主函数中调用了scnr.nextDouble();六次。确保在运行程序时提供六个double类型的参数。目前,您传递的参数少于六个,并且scnr.nextDouble();抛出异常,因为它找不到下一个 double 类型的参数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java