初学Java4:从键盘录入学生信息(仅姓名和成绩)并输出


//从键盘录入学生信息(仅姓名和成绩)并输出。
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
posted @ 2016-08-18 22:20  Azhi丶安之  阅读(11872)  评论(5编辑  收藏  举报