在一个for循环中找到数字的差异

huangapple go评论69阅读模式
英文:

Finding differences of numbers in a for loop

问题

System.out.print("输入旅行次数:");
carSample.numberOfTrips = input.nextInt();

int maximum = Integer.MIN_VALUE;
int minimum = Integer.MAX_VALUE;
int total = 0;

for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("里程表读数 " + (i + 1) + ": ");
    int odometerReading = input.nextInt();
    total += odometerReading;
    if (odometerReading > maximum) {
        maximum = odometerReading;
    }
    if (odometerReading < minimum) {
        minimum = odometerReading;
    }
}
英文:

I'm a beginner at java and was wondering on how to find the maximum difference between numbers inputted into a for loop. My program takes x amount of odometer readings (Between consecutive trips) from a car, e.g 100km, 150km, 400km, and is supposed to take the maximum distance traveled for all trips, which in this example would be 250km, as well as the minimum which would be 50km and average distance traveled between each odometer reading.

So far i've only managed to find a way to calculate the biggest value and smallest value for each odometer reading, given by the variables maximum and minimum, however have no idea on how to approach or begin to program finding the difference between trips. I tried implementing an array of some sort (not shown in this code) but i keep receiving too many errors. I could really use some advice on how to approach this problem or some insight; it would be greatly appreciated. Thank you for your time.

System.out.print(&quot;Input number of trips: &quot;); 	
carSample.numberOfTrips = input.nextInt();
	
	
	int maximum = Integer.MIN_VALUE;
	int minimum = Integer.MAX_VALUE;
	int total = 0;

for (int i = 0; i &lt; carSample.numberOfTrips; i++) {
    System.out.print(&quot;Odometer reading &quot; + (i + 1) + &quot;: &quot;);
    int odometerReading = input.nextInt();
	total += odometerReading;
	if (odometerReading &gt; maximum){
		maximum = odometerReading;
		}
	if (odometerReading &lt; minimum){
		minimum = odometerReading;	
	}

答案1

得分: 1

int previous = 0;
int minimumTrip = Integer.MAX_VALUE;
int maximumTrip = Integer.MIN_VALUE;

for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("里程表读数 " + (i + 1) + ": ");
    int odometerReading = input.nextInt();
    int currentTrip = odometerReading - previous;
    if (currentTrip > maximumTrip){
        maximumTrip = currentTrip;
    }
    if (currentTrip < minimumTrip){
        minimumTrip = currentTrip;  
    }
    previous = odometerReading;
}

如果读数不从0开始考虑将 previous 设为第一个里程表读数
英文:
int previous = 0;
int minimumTrip = Integer.MAX_VALUE;
int maximumTrip = Integer.MIN_VALUE;

for (int i = 0; i &lt; carSample.numberOfTrips; i++) {
    System.out.print(&quot;Odometer reading &quot; + (i + 1) + &quot;: &quot;);
    int odometerReading = input.nextInt();
    int currentTrip = odometerReading - previous;
    if (currentTrip &gt; maximumTrip){
        maximumTrip = currentTrip;
        }
    if (currentTrip &lt; minimumTrip){
        minimumTrip = currentTrip;  
    }
    previous = odometerReading;
}

If reading starts not from 0 consider previous = first odometer reading

答案2

得分: 0

Sure, here is the translated content:

如果您想计算每个里程数的最大值和最小值,两者的初始值都是0,每次读取一个里程数值时都会更新最大值和最小值,可以通过循环获得所有的里程数值。

如果您想计算所有里程数值的平均值,那么将所有值相加,然后除以值的数量即可。例如,平均值 = (a + b + c) / 3。

英文:

If you want to calculate the maximum and minimum values of each mileage, the initial values of both are 0, and the maximum and minimum values are updated every time a mileage value is read, and all the mileage values can be obtained by looping .

If you want to calculate the average of all mileage values, then add up all the values divided by the number of values
For example, av = (a + b + c) / 3

huangapple
  • 本文由 发表于 2020年4月9日 16:22:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61116859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定