//從鍵盤錄入學生信息(僅姓名和成績)並輸出。public class StuInformation {//此處命名用StuScore可能更恰當 String name; double score;}import java.util.Scanner;
public class TextStuInformation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 0, j = 0;
// 信息錄入
System.out.print("請輸入您要錄入的學生個數:");
int num = sc.nextInt();
StuInformation Stu[] = new StuInformation[num];
for (i = 0; i < Stu.length; i++) {
Stu[i] = new StuInformation();
System.out.print("請輸入第" + (i + 1) + "個學生的姓名:");
Stu[i].name = sc.next();
System.out.print("請輸入第" + (i + 1) + "個學生的成績:");
Stu[i].score = sc.nextDouble();
}
// 信息輸出
System.out.println("\t姓名\t成績");
for (StuInformation stuInformation : Stu) {
System.out.println("\t" + stuInformation.name + "\t" + stuInformation.score);
}
double max = 0, min = 0;
int cont = 0;
// 判斷最高分
for (i = 0; i < Stu.length; i++) {
if (max < Stu[i].score) {
max = Stu[i].score;
cont = i;//用來記錄最高成績的同學的name
}
}
System.out.println("最高分是:" + Stu[cont].name +","+ max);
// 判斷最低分
min = Stu[0].score;
for (j = 0; j < Stu.length; j++) {
if (min >= Stu[j].score) {
min = Stu[j].score;
cont = j;//用來記錄最低成績的同學的name
}
}
System.out.println("最低分是:" + Stu[cont].name +","+ min);
sc.close();
}
}
結果預覽:
A_zhi
2016.08.18.22.00