import java.util.Scanner;
public class RankOfStudents {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the numbers of students: ");
int numbers = input.nextInt();
inputName(numbers);
inputScore(numbers);
rank(inputName(numbers), inputScore(numbers), numbers);
}
public static String[] inputName(int numbers) {
Scanner input = new Scanner(System.in);
String[] name = new String[30];
int i;
for (i = 0; i < numbers; i++) {
System.out.print("Enter the name of student" + i + ": ");
name[i] = input.nextLine();
}
return name;
}
public static double[] inputScore(int numbers) {
Scanner input = new Scanner(System.in);
double[] score = new double[numbers];
int i;
for (i = 0; i < numbers; i++) {
System.out.print("Enter the score of student" + i + ": ");
score[i] = input.nextDouble();
}
return score;
}
public static void rank(String[] name, double[] score, int numbers) {
int i = 0;
String middle;
double change;
for (i = 0; i < numbers; i++) {
if (score[i] < score[i + 1]) {
change = score[i + 1];
score[i + 1] = score[i];
score[i] = change;
middle = name[i + 1];
name[i + 1] = name[i];
name[i] = middle;
}
}
for (int j = 0; j < numbers; j++) {
System.out.print(name[i] + " ");
System.out.println("");
System.out.print(score[i] + " ");
System.out.println(" ");
}
}
}
------解决方案--------------------------------------------------------
inputName(numbers);
inputScore(numbers);
rank(inputName(numbers), inputScore(numbers), numbers);
这里有问题,他会重复调用两次。
for (i = 0; i < numbers; i++) {
if (score[i] < score[i + 1]) {
change = score[i + 1];
score[i + 1] = score[i];
score[i] = change;
middle = name[i + 1];
name[i + 1] = name[i];
name[i] = middle;
}
}
这个循环也有问题,会越界的。
改成:
rank(inputName(numbers), inputScore(numbers), numbers);
for (i = 0; i < numbers-1; i++) {
if (score[i] < score[i + 1]) {
change = score[i + 1];
score[i + 1] = score[i];
score[i] = change;
middle = name[i + 1];
name[i + 1] = name[i];
name[i] = middle;
}
}
------解决方案--------------------------------------------------------
- Java code
for (i = 0; i < numbers-1; i++) { if (score[i] < score[i + 1]) { change = score[i + 1]; score[i + 1] = score[i]; score[i] = change; middle = name[i + 1]; name[i + 1] = name[i]; name[i] = middle; } }
------解决方案--------------------------------------------------------
- Java code
import java.util.Scanner;public class RankOfStudents { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the numbers of students: "); int numbers = input.nextInt(); String[] names = inputName(numbers); double[] scores =inputScore(numbers); rank(names, scores, numbers); //rank(inputName(numbers), inputScore(numbers), numbers); //直接放进去 ,否则会调用两次 } public static String[] inputName(int numbers) { Scanner input = new Scanner(System.in); String[] name = new String[30]; int i; for (i = 0; i < numbers; i++) { System.out.print("Enter the name of student" + i + ": "); name[i] = input.nextLine(); } return name; } public static double[] inputScore(int numbers) { Scanner input = new Scanner(System.in); double[] score = new double[numbers]; int i; for (i = 0; i < numbers; i++) { System.out.print("Enter the score of student" + i + ": "); score[i] = input.nextDouble(); } return score; } public static void rank(String[] name, double[] score, int numbers) { int i = 0; String middle; double change; //冒泡排序一下 for(int k = 0 ; k < numbers; k++){ for (i = k+1 ; i < numbers; i++) { if (score[k] < score[i]) { System.out.println(score[k] + " " + score[i]) ; change = score[i]; score[i] = score[k]; score[k] = change; //System.out.println(score[i] + " " + score[i + 1]) ; middle = name[i]; name[i] = name[k]; name[k] = middle; } } } for (int j = 0; j < numbers; j++) { System.out.println("student - " + j +" :") ; System.out.print(name[j] + " "); //System.out.print(name[i] + " "); i 应该为 j System.out.println(""); System.out.print(score[j] + " "); //System.out.print(score[i] + " "); i 应该为 j System.out.println(" "); } }}