为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红颜莎娜 2023-07-19 10:11:32
我不断收到 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
查看完整描述

4 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

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

   }

}


查看完整回答
反对 回复 2023-07-19
?
大话西游666

TA贡献1817条经验 获得超14个赞

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

   }

}


查看完整回答
反对 回复 2023-07-19
?
森林海

TA贡献2011条经验 获得超2个赞

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

  

    }

}


查看完整回答
反对 回复 2023-07-19
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

您在主函数中调用了scnr.nextDouble();六次。确保在运行程序时提供六个double类型的参数。目前,您传递的参数少于六个,并且scnr.nextDouble();抛出异常,因为它找不到下一个 double 类型的参数。



查看完整回答
反对 回复 2023-07-19
  • 4 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信