java。 急 ! 编程语言!

2024-11-29 05:40:11
推荐回答(3个)
回答1:

import java.util.Arrays;
import java.util.Random;
public class RandScoreTest {
private int[] scoresA;
private int[] scoresB;
private int[] scoresTotal;
private int scoreMax;
private transient Random random = new Random();
public static void main(String[] args) {
RandScoreTest randScoreTest = new RandScoreTest(150, 180, 50);
System.out.println("scores A:");
randScoreTest.printScoresA();
System.out.println("scores B:");
randScoreTest.printScoresB();
System.out.println("scores Total:");
randScoreTest.printScoresTotal();
randScoreTest.shuttleSortHighToLow();
System.out.println("scores Total low to high:");
randScoreTest.printScoresTotalInvert();
System.out.println("scores Total high to low:");
randScoreTest.printScoresTotal();
System.out.println("highest score:");
System.out.println(randScoreTest.getMaxScore());
System.out.println("lowest score:");
System.out.println(randScoreTest.getMinScore());
System.out.println("average score:");
System.out.println(randScoreTest.getAverageScore());
System.out.println("standard deviation:");
System.out.println(randScoreTest.standardDeviation());
}
public RandScoreTest(int aCount, int bCount, int scoreMax) {
scoresA = new int[aCount];
scoresB = new int[bCount];
scoresTotal = new int[aCount > bCount ? aCount : bCount];
this.scoreMax = scoreMax;
generateScores(scoresA);
generateScores(scoresB);
}
private void generateScores(int[] scores) {
for (int i = 0, len = scores.length; i < len; i++) {
scores[i] = random.nextInt(scoreMax);
scoresTotal[i] += scores[i];
}
}
public void printScoresA() {
printScores(scoresA);
}
public void printScoresB() {
printScores(scoresB);
}
public void printScoresTotal() {
printScores(scoresTotal);
}
private void printScores(int[] scores) {
System.out.println(Arrays.toString(scores));
}
public void printScoresTotalInvert() {
StringBuilder buf = new StringBuilder();
for (int i = scoresTotal.length - 1; i >= 0; i--) {
buf.append(scoresTotal[i]).append(",");
}
System.out.println(buf.toString());
}
public void shuttleSortHighToLow() {
for (int i = 0; i < scoresTotal.length - 1; i++) {
for (int j = i + 1; j < scoresTotal.length; j++) {
if (scoresTotal[i] <= scoresTotal[j]) {
int temp = scoresTotal[i];
scoresTotal[i] = scoresTotal[j];
scoresTotal[j] = temp;
}
}
}
}
public int getMaxScore() {
return scoresTotal[0];
}
public int getMinScore() {
return scoresTotal[scoresTotal.length - 1];
}
public int getAverageScore() {
int sum = 0;
for (int i = 0, len = scoresTotal.length; i < len; i++) {
sum += scoresTotal[i];
}
return sum / scoresTotal.length;
}
public double standardDeviation() {
long sum1 = 0;
int sum2 = 0;
int len = scoresTotal.length;
for (int i = 0; i < len; i++) {
sum1 += scoresTotal[i] * scoresTotal[i];
sum2 += scoresTotal[i];
}
return Math.pow((sum1 - sum2 * sum2 / len) / (len - 1), 0.5);
}
}

回答2:

代码太长,留邮箱吧

回答3:

这么多,彩30分