用户输入问题的代码双方程错误

时间:2019-01-11 22:13:39

标签: java math input equation

我的第一个任务是开发一个代码,该代码使用户可以输入他们希望行驶的距离(英里),燃油效率和汽油成本的数据。然后创建代码以计算行程的总费用。

我具有所有输入值的所有代码,但方程式本身存在问题。 Java无法识别“ /”。除非需要添加更多代码以使方程起作用,否则我无法理解我在做什么。

import java.util.Scanner;
public class DrivingCost
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        System.out.print("Please enter your distance (miles): ");

      Scanner t = new Scanner(System.in);
      System.out.print("Please enter vehicle's fuel efficiency (mpg): ");

      Scanner u = new Scanner(System.in);
      System.out.print("Please enter the price per gallon (dollars): ");

      String distanceInMiles = s.nextLine();
      System.out.println("The distance (miles): " + distanceInMiles);

      String fuelEfficiency = t.nextLine();
      System.out.println("Fuel efficiency (mpg):" + fuelEfficiency);

        String pricePerGallon = u.nextLine();
        System.out.println("Price per gallon (dollars): " + pricePerGallon);

      double tripCost = (distanceInMiles / fuelEfficiency) * pricePerGallon;
      System.out.println("The trip cost (dollars): " + tripCost);
    }
} 

这是我一直收到的错误:

DrivingCost.java:32: error: bad operand types for binary operator '/'
  double tripCost = (distanceInMiles / fuelEfficiency) * pricePerGallon;
                                     ^

3 个答案:

答案 0 :(得分:0)

您可以使用distanceInMilesfuelEfficiency中获得pricePerGallondoubles.nextDouble()。 之后,您应该能够对这些变量执行double操作。

public static void main(String args[])
  {
    Scanner s = new Scanner(System.in);

    System.out.print("Please enter your distance (miles): ");
    double distanceInMiles = s.nextDouble();
    System.out.println("The distance (miles): " + distanceInMiles);

    System.out.print("Please enter vehicle's fuel efficiency (mpg): ");
    double fuelEfficiency = s.nextDouble();
    System.out.println("Fuel efficiency (mpg):" + fuelEfficiency);

    System.out.print("Please enter the price per gallon (dollars): ");
    double pricePerGallon = s.nextDouble();
    System.out.println("Price per gallon (dollars): " + pricePerGallon);

    double tripCost = (distanceInMiles / fuelEfficiency) * pricePerGallon;

    System.out.println("The trip cost (dollars): " + tripCost);
  }

答案 1 :(得分:0)

您正在尝试使用字符串进行计算。您必须从字符串输入中解析出double。只需将等式行更改为此:

double tripCost = (Double.valueOf(distanceInMiles) / Double.valueOf(fuelEfficiency)) * Double.valueOf(pricePerGallon);

P.S。正确的输入验证将是一个很好的改进。万一用户输入错误。另外,如评论中所述,无需使用多个Scanners。一个就足够了。

答案 2 :(得分:0)

  1. 您正在对String进行数学运算,不能,您需要double类型
    • Double.parseDouble(sc.nextLine());读取一行并解析为double(优点:一般情况下避免返回行错误,养成良好习惯)
    • sc.nextDouble()直接读取double
  2. 每个来源仅使用一个扫描仪
  3. print和扫描仪询问之间保持良好的顺序
Scanner sc = new Scanner(System.in);

System.out.print("Please enter your distance (miles): ");
String distanceInMiles = Double.parseDouble(sc.nextLine());
System.out.println("The distance (miles): " + distanceInMiles);

System.out.print("Please enter vehicle's fuel efficiency (mpg): ");
String fuelEfficiency = Double.parseDouble(sc.nextLine());
System.out.println("Fuel efficiency (mpg):" + fuelEfficiency);

System.out.print("Please enter the price per gallon (dollars): ");
String pricePerGallon = Double.parseDouble(sc.nextLine());
System.out.println("Price per gallon (dollars): " + pricePerGallon);

double tripCost = (distanceInMiles / fuelEfficiency) * pricePerGallon;
System.out.println("The trip cost (dollars): " + tripCost);