所以程序应该允许用户输入 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;
}
}
}