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

问题描述 投票:0回答:5

我不断收到 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

java exception
5个回答
2
投票
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);
   }
}

1
投票

drivenMiles 除以英里每加仑,然后乘以美元每加仑,即可得出每英里行驶的汽油价格。 注意:在这种情况下,drivenMiles 只需要传递给 movingCost。这就是为什么要添加整数 10、50 和 400 来调用。

由于 DrivingCost 具有按顺序排列的参数milesPerGallon、dollarsPerGallon 和drivenMiles,因此您必须以相同的参数顺序调用该方法。

“%.2f”会保留两位小数。添加 之后将开始新的一行。

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


0
投票

您在主函数中调用了

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


0
投票
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));
  
    }
}

0
投票

对于那些因某种原因遇到我在这个实验室中遇到的奇怪问题的人,哈哈。这对我有用。

import java.util.Scanner;

public class LabProgram {

  public static double drivingCost(double milesPerGallon, double dollarsPerGallon, double milesDriven) {
     double Cost = (milesDriven / milesPerGallon) * dollarsPerGallon;
     return Cost;
  }

  public static void main(String[] args) {
     Scanner scnr = new Scanner(System.in);
     double milesPerGallon;
     double dollarsPerGallon;
     double milesDriven;
  
     milesPerGallon = scnr.nextDouble();
     dollarsPerGallon = scnr.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));
  

} }

© www.soinside.com 2019 - 2024. All rights reserved.