package 實驗報告2;import java.util.Scanner;public class student { // 創建Sutdent類 public static class Student { int stuScore; String stuName; } public static void main(String[] args) { Student[] stu = new Student[10]; Scanner input = new Scanner(System.in); // 錄入用戶輸入信息 for (int i = 1; i < stu.length; i++) { stu[i] = new Student(); System.out.println("請輸入第" + i++ + "學生姓名:"); stu[i].stuName = input.next(); System.out.println("請輸入學生姓名:"); stu[i].stuScore = input.nextInt(); } //輸出學生信息 System.out.println("學生信息為:"); for (int i = 0; i < stu.length; i++) { System.out.print(stu[i].stuName + stu[i].stuScore); } // 使用for循環,對學生信息進行排序 for (int i = 0; i < stu.length - i - 1; i++) { if (stu[i].stuScore < stu[i + 1].stuScore) { int temp; temp = stu[i].stuScore; stu[i].stuScore = stu[i + 1].stuScore; stu[i + 1].stuScore = temp; } } //輸出排序后學生信息 System.out.println("排序后學生信息為:"); for (int i = 0; i < stu.length; i++) { System.out.print(stu[i].stuName + stu[i].stuScore); } }}
添加回答
舉報
0/150
提交
取消