简体   繁体   中英

Loop and display confusion

I apologize if this seems like a simple question, but I have a problem I'm trying to solve and haven't quite gotten my head around how to display to results properly. So basically its asking to accept user input for a car traveling a certain distance. So I used a scanner object to get the time (in hrs) as well as the speed(mps). so to get the distance you simply multiply these 2 values. But my problem is in the display, I used a for loop to display how much distance the user traveled for every hour (starting from 1 - until it has reached the time the user inputs) the for loop looks like this:

    // Calculate and display distance traveled
    distance = speed * time;
    System.out.println("Hour Distance Traveled");
    System.out.println("--------------------------");

    for (time =1; time <= distance/speed; time++)
    {
             System.out.println(time + "\t" + distance);
    }

So I'm obviously stuck on how to display the distance in incremented form, like how time is displayed, I'm not sure how to go about it, and more importantly if a for loop is the appropriate loop to use in this case. I'm not asking to get the problem done for me but I would appreciate any advice or explanation on how to go about solving it or what I'm doing wrong.

In each iteration of the loop you want to multiply the time ( in current iteration ) by the speed, not to use the final calculated distance.

So:

System.out.println("" + time + "\t" + ( time * speed ) );

Should work for you.

EDIT Elaborating a little to answer the comment

You perform the same task twice. Once when calculating the result and one for tracing. You can connect the two and this way show the progress as it is calculated. Of course, if you don't need to print anything, you can just perform the multiplication as you did...

/* time and speed received from user. */
/* initialize distance */
distance = 0;
for (int i = 1; i <= time; i++)
{
    /* increment distance in each iteration */
    distance += speed;
    /* print as you progress */
    System.out.println(i + "\t" + distance);
}

I used a scanner object to get the time (in hrs) as well as the speed(mps).

Well, If i understood you correctly, this is probably what you want:

for (time =1; time <= inputTime; time++)
    {
            distance = inputSpeed * time;
             System.out.println(time + "\t" + distance);
    }
for (time =1; time <= endTime; time++)
{
         distance = speed * time;
         System.out.println(time + "\t" + distance);
}

you just need to update the distance in the loop

You're running into confusion because you're trying to reuse the same variables "time" and "distance" for two different things in the same block of code - both for the entire trip, and also for each hour block. Use different variable names, and it becomes so much easier.

So, you read "time" and "distance" for a journey - so that's "tripHours" and "tripDistance".

In your loop, you're counting up the hours, so you could have something like :

int tripDistance = speed * tripHours;
for (int hour = 1; hour <= tripHours; hour++)
    int distance = hour * speed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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