Java 如何使用行驶的时间和距离获得平均速度

Java 如何使用行驶的时间和距离获得平均速度,java,Java,这个代码确实有效,但我不确定我是否从中得到了正确的答案。我是否使用了错误的公式或其他什么?我把论坛放在代码末尾的一个双行注释中。请不要告诉我添加故障保险或任何我想让它不那么混乱的东西 public class ICS3URightOnJavaAssignment2c { public static void main (String[] args) throws Exception { BufferedReader buffer = new B

这个代码确实有效,但我不确定我是否从中得到了正确的答案。我是否使用了错误的公式或其他什么?我把论坛放在代码末尾的一个双行注释中。请不要告诉我添加故障保险或任何我想让它不那么混乱的东西

  public class ICS3URightOnJavaAssignment2c
  {
      public static void main (String[] args) throws Exception
      {
          BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

          String d; //creates a string variable to get distance (travelled) from user
          double distance = 0.0; //double distance
          System.out.print("Enter distance travelled(KM): ");  //gets distance travelled from user
          d = buffer.readLine(); //reads a line from the console
          distance = Double.parseDouble(d);//gets the double value from string variable d
          System.out.println("Distance Travelled: " + d); //states string variable d

          new BufferedReader(new InputStreamReader(System.in));

          String t; //creates a string variable to get time (travelled) from user
          double time = 0.0; //double time
          System.out.print("Enter time travelled(Minutes): "); //gets time travelled from user
          t = buffer.readLine(); //reads a line from the console
          time = Double.parseDouble(t);//gets the double value from string variable t
          System.out.println("Time Travelled: " + t); //states string variable t

          System.out.println("The average speed (KM/H) with a distance travelled of " + d + and time travelled of " + t + " is " + distance / time);

          /*speed (average) = distance divided by time*/      
      }
  }

您是否有任何理由相信输出错误,或者只是希望我们检查您的代码?如果是后者,不幸的是,这并不真正适用于(用于帮助您解决所发现的问题,而不是分析代码以找到问题)。但是是的,
距离/时间
是计算速度的方法。虽然
时间
以分钟为单位,而不是以小时为单位,因此您需要将
时间
/除以60。您的代码中有一对多余的
{}
,我已将其删除。和
新的BufferedReader(新的InputStreamReader(System.in))(它自己行上的代码)没有任何作用,应该将其删除。我缩进了你的代码,使它更可读。在最后一次打印中,还有一个“缺少”符号。