简体   繁体   English

循环和显示混乱

[英]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). 所以我使用扫描仪对象来获取时间(以小时为单位)以及速度(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: 但我的问题在于显示,我使用for循环显示用户每小时行进的距离(从1开始 - 直到达到用户输入的时间)for循环如下所示:

    // 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. 所以我显然坚持如何以递增的形式显示距离,比如时间显示,我不知道如何去做,更重要的是如果for循环是在这种情况下使用的适当循环。 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). 我使用扫描仪对象来获取时间(以小时为单位)以及速度(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. 你正在陷入混乱,因为你试图在同一个代码块中为两个不同的东西重复使用相同的变量“time”和“distance” - 既可以用于整个行程,也可以用于每个小时块。 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". 所以,你读了一段旅程的“时间”和“距离” - 这就是“tripHours”和“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;

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

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