したがって、このプログラムでは、ユーザーが 10 個のスコアを入力し、それらのスコアを次の行に昇順に出力できるようになっています。何らかの理由でスコアを入力できますが、次の行は入力を昇順にソートするのではなく、0 で埋められています。理由はわかりませんが、どんな入力でも役に立ちます。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out
.println("Enter up to 35 scores between 0 and 100, -1 to stop:");
int[] score = new int[35];
int count = 0;
int sum = 0;
String scores = "";
for (int i = 0; i < score.length; i++) {
score[i] = keyboard.nextInt();
if (score[i] >= 0) {
scores = scores + score[i] + " ";
count++;
sum = sum + score[i];
} else
i = score.length + 1;
}
for (int i = 1; i < score.length; i++) {
int x;
int temp;
x = score[i];
temp = score[i];
for (x = i - 1; x >= 0 && score[x] > temp; x--) {
score[x + 1] = score[x];
}
score[x + 1] = temp;
}
System.out.printf("The %d scores you entered are: \n%s", count, scores);
System.out.println();
System.out.printf("The %d scored sorted in nondecreasing order are:\n",
count);
int k=1;
for (k=1; k <= count; k++) {
if (k % 11 == 0) {
System.out.println();
} else {
System.out.printf("%5d", score[k]);
}
}
for (int i = 1; i <= count; i++) {
if (i % 11 == 0) {
System.out.println();
} else {
System.out.printf("%5d", score[i]);
}
for (int j = 1; j < count; j++) {
if (Integer.compare(score[i], score[j]) < 0) {
int temp = score[i];
score[i] = score[j];
score[j] = temp;
}
}
}