exercise5_16.java
来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 52 行
JAVA
52 行
// Exercise5_16.java:
import javax.swing.JOptionPane;
public class Exercise5_16 {
public static void main(String[] args) {
// Prompt the user to enter the number of students
String numberString =
JOptionPane.showInputDialog("Enter the number of students");
int numberOfStudents = Integer.parseInt(numberString);
String[] names = new String[numberOfStudents];
double[] scores = new double[numberOfStudents];
// Enter student name and score
for (int i = 0; i < scores.length; i++) {
names[i] = JOptionPane.showInputDialog("Enter a student name");
scores[i] = Double.parseDouble(
JOptionPane.showInputDialog("Enter a student score"));
}
// Sort
for (int i = scores.length - 1; i >= 1; i--) {
// Find the maximum in the scores[0..i]
double currentMax = scores[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++) {
if (currentMax < scores[j]) {
currentMax = scores[j];
currentMaxIndex = j;
}
}
// Swap scores[i] with scores[currentMaxIndex] if necessary;
// Swap names[i] with names[currentMaxIndex] if necessary;
if (currentMaxIndex != i) {
scores[currentMaxIndex] = scores[i];
scores[i] = currentMax;
String temp = names[currentMaxIndex];
names[currentMaxIndex] = names[i];
names[i] = temp;
}
}
// Print names and scores
for (int i = scores.length - 1; i >= 0; i--) {
System.out.println(names[i] + "\t" + scores[i]);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?