输出到控制台:
Enter the race times (in seconds):
1 1 4
First place (time in seconds): 1.0
Second place (time in seconds): 1.0
Third place (time in seconds): 4.0
The range of the race times (in seconds): 0.0
The average time of all racers (in seconds): 0.00
Enter another race? (y/n):
我的代码:
public static void sortTimesAscending(){// Algorithm method that sorts and calculates the whole process (in seconds).
//Sorting calculation.
max = Math.max(Math.max(time1, time2), time3);
min = Math.min(Math.min(time1, time2), time3);
double mid = time1 + time2 + time3 - max - min;
//Assigning sorted calculation.
double firstPlace = min;
double secondPlace = mid;
double thirdPlace = max;
//Calculating range of the race times and the average of the race times of all three racers.
double range = max - min;
double average = (time1 + time2 + time3) / 3;
//Displaying racers' positions.
System.out.printf("\nFirst place (time in seconds): %4.1f%n", firstPlace);
System.out.printf("Second place (time in seconds): %4.1f%n", secondPlace);
System.out.printf("Third place (time in seconds): %4.1f%n", thirdPlace);
//Determining whether any racers were tied for first or second positions and displaying the rest of the results.
if (time1 == time2 && time2 == time3){
System.out.println("\nAll racers shared first place.");
System.out.printf("The range of race times (in seconds): %4.1f%n", range);
System.out.printf("The average time of racers (in seconds): %4.1f%n", average);
}
else if ((time1 == time2 && time2 > time3) ||
(time2 == time3 && time2 > time1) ||
(time1 == time3 && time1 > time2)){
System.out.println("\nTwo racers shared second place.");
System.out.printf("The range of race times (in seconds): %4.1f%n", range);
System.out.printf("The average time of racers (in seconds): %4.1f%n", average);
}
else if ((time1 == time2 && time2 < time3) ||
(time2 == time3 && time2 < time1) ||
(time1 == time3 && time1 < time1)){
System.out.println("\nTwo racers shared first place.");
System.out.printf("The range of race times (in seconds): %4.1f%n", range);
System.out.printf("The average time of racers (in seconds): %4.1f%n", average);
}
else {
System.out.printf("\nThe range of race times (in seconds): %4.1f%n", range);
System.out.printf("The average time of racers (in seconds): %4.1f%n", average);
}
}
//getter methods for race times, range, and average
public double[] getTimes() {
double[] times = {time1, time2, time3};
return times;
}
public double getRange() {
return range;
}
public double getAvg() {
return total / 3;
}
我不知道我的代码有什么问题,它没有将范围和平均值的输出发送到控制台
我尝试了一切,但没有任何效果。请在这种情况下帮助我。
这是代码的修订版本,已解决这些问题:
public class RaceAnalyzer {
private double time1;
private double time2;
private double time3;
private double max;
private double min;
private double range;
private double total;
public void setTimes(double t1, double t2, double t3) {
time1 = t1;
time2 = t2;
time3 = t3;
total = t1 + t2 + t3;
calculateStatistics();
}
private void calculateStatistics() {
max = Math.max(Math.max(time1, time2), time3);
min = Math.min(Math.min(time1, time2), time3);
double mid = total - max - min;
range = max - min;
// You can calculate the average here, (if you want)
}
public void displayResults() {
System.out.printf("\nFirst place (time in seconds): %4.1f%n", min);
System.out.printf("Second place (time in seconds): %4.1f%n", total - max - min);
System.out.printf("Third place (time in seconds): %4.1f%n", max);
// Display other results
System.out.printf("The range of race times (in seconds): %4.1f%n", range);
System.out.printf("The average time of racers (in seconds): %4.1f%n", total / 3);
}
// Your getter methods here
public static void main(String[] args) {
RaceAnalyzer analyzer = new RaceAnalyzer();
analyzer.setTimes(1, 1, 4);
analyzer.displayResults();
}
}
在此代码中,我在名为
RaceAnalyzer
的类的方法中组织了计算和显示逻辑。 setTimes()
方法设置比赛时间,然后调用calculateStatistics()
方法进行计算。 displayResults()
方法显示结果,包括范围和平均值。 main
方法展示了如何使用此类来分析和显示比赛结果。