英文:
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("Input number of trips: ");
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("Odometer reading " + (i + 1) + ": ");
int odometerReading = input.nextInt();
total += odometerReading;
if (odometerReading > maximum){
maximum = odometerReading;
}
if (odometerReading < 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 < carSample.numberOfTrips; i++) {
System.out.print("Odometer reading " + (i + 1) + ": ");
int odometerReading = input.nextInt();
int currentTrip = odometerReading - previous;
if (currentTrip > maximumTrip){
maximumTrip = currentTrip;
}
if (currentTrip < 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论