これが私の現在のコードであり、これが私の現在の出力です:
This program will calculate an overall score for a diver, based on individual dives.
The diver's score for dive 1 is 79.80.
The diver's score for dive 2 is 49.80.
The average score for these dives is 64.80.
こちらが仕様のページです。
基本的に、このプログラムは次のようなファイルから文字列を取得します。
1 2.5 8.5 8.0 8.5 9.5 9.0 7.5 6.5
ここで、1
はダイビング番号、2.5
は難易度、残りはスコアです。最高と最低を除くすべてのスコアを合計し、難易度を掛け、0.6を掛けてダイビングのスコアを取得します。
私が問題を抱えているのはこれです:私のダイブ2のスコアは正しいですが、私のダイブ1のスコアは正しくありません。理由はよくわかりません。
私は本当に助けていただければ幸いです。私はそれがなぜこのようになっているのかを理解するために頭を悩ませましたが、私はそれを理解できないようです。ご覧いただきありがとうございます、ありがとうございます。
編集:これがあなたたちのためのコードです:
import java.util.*;
import java.io.*;
public class Dive {
public static void main(String[] args) throws FileNotFoundException {
printIntro();
Scanner fileScanner = new Scanner(new File("DiveData.txt"));
processDives(fileScanner);
}
public static void printIntro() {
System.out
.println("Welcome to the Diver Scoring program."
+ "\nThis program will calculate an overall score for a diver, based on individual dives.");
}
public static void processDives(Scanner fileScanner) {
double avg = 0;
int count = 0;
while (fileScanner.hasNext()) {
int diveNumber = fileScanner.nextInt();
String diveLine = fileScanner.nextLine();
double score = calculateDiveScore(diveLine);
avg += score;
System.out.printf("The diver's score for dive " + diveNumber
+ " is " + "%.2f. \n", score);
count++;
diveNumber = 0;
}
System.out.printf("\nThe average score for these dives is "
+ "%.2f. \n", avg / (double) count);
}
public static double calculateDiveScore(String diveLine) {
double score = 0.0;
String subNumbers = "";
double difficulty = Double.parseDouble(diveLine.substring(1, 4));
diveLine = diveLine.substring(5);
double max = -500, min = 500;
for (int i = 0; i < diveLine.length(); i++) {
// if we're getting something other than a space, add it to the
// string. We'll do the calculations while we have spaces.
if (diveLine.charAt(i) != ' ') {
subNumbers += diveLine.charAt(i);
} else {
double val = Double.parseDouble(subNumbers);
// if the score is neither largest nor smallest, add it to total
// score.
if (Math.max(val, max) == max && Math.min(val, min) == min) {
score += val;
}
// if it is either largest or smallest, add previous largest or
// smallest and set current to largest or smallest.
if (Math.max(val, max) == val) {
if (max != -500)
score += max;
max = val;
} else if (Math.min(val, min) == val) {
if (min != 500)
score += min;
min = val;
}
subNumbers = "";
}
}
//check the last number to see if it's a max or a min
if (Math.max(Double.parseDouble(subNumbers), max) == Double
.parseDouble(subNumbers)) {
score += max;
} else if (Math.min(Double.parseDouble(subNumbers), min) == Double
.parseDouble(subNumbers)) {
score += min;
}
return score * difficulty * 0.6;
}
}
そして答えはここにあります:
import java.util.*;
import java.io.*;
public class Dive {
public static void main(String[] args) throws FileNotFoundException {
printIntro();
Scanner fileScanner = new Scanner(new File("DiveData.txt"));
processDives(fileScanner);
}
public static void printIntro() {
System.out
.println("Welcome to the Diver Scoring program."
+ "\nThis program will calculate an overall score for a diver, based on individual dives.");
}
public static void processDives(Scanner fileScanner) {
double avg = 0;
int count = 0;
while (fileScanner.hasNext()) {
int diveNumber = fileScanner.nextInt();
String diveLine = fileScanner.nextLine();
double score = calculateDiveScore(diveLine);
avg += score;
System.out.printf("The diver's score for dive " + diveNumber
+ " is " + "%.2f. \n", score);
count++;
diveNumber = 0;
}
System.out.printf("\nThe average score for these dives is " + "%.2f.",
avg / (double) count);
}
public static double calculateDiveScore(String diveLine) {
diveLine = diveLine.substring(1);
String[] fields = diveLine.split(" ");
double difficulty = Double.parseDouble(fields[0]);
double[] scores = new double[fields.length - 1];
double max = -500.0, min = 500.0, score = 0;
for (int i = 0; i < fields.length - 1; i++) {
scores[i] = Double.parseDouble(fields[i + 1]);
}
for (int i = 0; i < scores.length; i++) {
if (Math.max(scores[i], max) == scores[i])
max = scores[i];
if (Math.min(scores[i], min) == scores[i])
min = scores[i];
score += scores[i];
}
score -= max;
score -= min;
return score * difficulty * 0.6;
}
}