简体   繁体   English

距离旅行循环帮助Java

[英]Distance Traveled loop help Java

I've been racking my brain trying to figure out why my program isn't looping right. 我一直在绞尽脑汁想弄清楚为什么我的程序循环不正确。 Rather than getting each hour increments I'm only getting the final hour/speed. 而不是获得每个小时的增量,而是获得最终的小时/速度。 So if I put that I'm going 40 mph for 3 hours I only get 120. 因此,如果我以40英里/小时的速度行驶3个小时,我只会得到120英里/小时。

Here is the problem: 这是问题所在:

The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. 可以计算出车辆行驶的距离:距离=速度*时间例如,如果火车以每小时40英里的速度行驶3个小时,则行驶的距离为120英里。 Write a program that asks for the speed of a vehicle (in mph) and the number of hours it has traveled. 编写一个程序,询问车辆的速度(以英里/小时为单位)以及行驶的小时数。 It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. 它应该使用一个循环来显示用户指定时间段内每小时行驶的车辆的距离。 For example, if a vehicle is traveling at 40 mph for a three-hour time period, it should display a report similar to the one that follows: Hour Distance Traveled 例如,如果车辆以40 mph的速度行驶了三个小时,则它应显示类似于以下的报告:行驶时数

1 40 1 40

2 80 2 80

3 120 3 120

import java.util.Scanner;

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

  int time , speed ,  hour;
  double  distance;

  System.out.println("How fast were you going ?");
  speed = keyboard.nextInt();

  while(speed<=0)
  {
     System.out.println("Please enter a valid speed ");
     speed = keyboard.nextInt();
  }

  System.out.println(" How long did you ride / drive ?");
  time = keyboard.nextInt();

  while(time<=0)
  {
     System.out.println("Please enter a valid time ");
     time = keyboard.nextInt();
  }

  System.out.println(" Hour                         Distance");
  System.out.println("---------------------------------");
  hour = 0;


  for( int x = 1; x<=time; x++)
  {
     hour++;
     if(hour>1)
     {
        distance = time * speed;
        System.out.println(time+ "                " +distance);
     }
    }
   }
  }

use hour inside your loop instead of time, 在循环中使用小时代替时间,

 for( int x = 1; x<=time; x++) {
     hour++;
     if(hour>1) {
         distance = hour * speed;
         System.out.println(hour+ "                " +distance);
     }
 }

or just use x 或只使用x

Delete the variables hour and distance . 删除hourdistance变量。

Change the name of the loop variable from x to hour . 将循环变量的名称从x更改为hour

Simply your loop to: 只需循环即可:

for( int hour = 1; hour <= time; hour++)
    System.out.println(hour+ " " + (hour * speed));

Note that if you did used your distance variable, it was a double which is the wrong type, because: 请注意,如果您确实使用过distance变量,则它是一个double精度类型错误的double精度型,因为:

  • both speed and time are int , so distance must be int 速度和时间都是int ,所以距离必须是int
  • the output of a double has a decimal point, but your requirements did not show one double精度输出的结果带有小数点,但您的要求未显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM