//文件名:Student.java
//包名:personal.studentManager.com
package personal.studentManager.com;
import java.util.Scanner;
/*
* 定义学生类,包括属性:学号,姓名,语文,数学;包括方法:构造方法,完成属性赋值,
* 能修改属性
* 录入成绩
* 能按学号查询成绩。
* 班级共有10名学生,请利用类及数组完成相关代码。*/
public class Student {
Scanner input = new Scanner(System.in); //输入类
//定义属性
String m_sID;
String m_sName;
double m_mathScore;
double m_chineseScore;
//有参构造直接赋值
public Student(String sID, String sName, double mathScore, double chineseScore) {
this.m_sID = sID;
this.m_sName = sName;
this.m_mathScore = mathScore;
this.m_chineseScore = chineseScore;
}
//无参构造直接赋值为默认值
public Student() {
this.m_sID = "Default sID";
this.m_sName = "Default sName";
this.m_mathScore = 0.0;
this.m_chineseScore = 0.0;
}
//更改方法
void updateInfo() {
System.out.println("请输入要更改的学号");
this.m_sID = input.next();
System.out.println("请输入要更改的姓名");
this.m_sName = input.next();
System.out.println("请输入要更改的语文成绩");
this.m_chineseScore = input.nextDouble();
System.out.println("请输入要更改的数学成绩");
this.m_mathScore = input.nextDouble();
System.out.println(this.m_sName+"学生更改完成");
}
//录入成绩方法
void updateScore() {
System.out.println("请输入学生的语文成绩");
this.m_chineseScore = input.nextDouble();
System.out.println("请输入学生的数学成绩");
this.m_mathScore = input.nextDouble();
}
//显示信息方法
void showStudentInfo() {
System.out.println("该学生的姓名为"+this.m_sName);
System.out.println("该学生的语文成绩为"+this.m_chineseScore);
System.out.println("该学生的数学成绩为"+this.m_mathScore);
}
//重载显示信息方法,直接传参赋值
void updateScore(double mathScore,double chineseScore) {
this.m_chineseScore = chineseScore;
this.m_mathScore = mathScore;
}
int getStudentIntID() {
return Integer.parseInt(this.m_sID); //返回int类型的学号,以便后续维护排序
}
String getStudentStrId() { //返回str类型的学号,以便后续维护查找
return this.m_sID;
}
}
//文件名:Manager.java
//包名:personal.studentManager.com
//主函数入口
package personal.studentManager;
import java.util.Scanner;
public class Manager {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
Student studentArr[] = new Student[10];
for(int i = 0; i < 10; i++) {
studentArr[i] = new Student(); //在堆区创建对象的构造
}
search(studentArr); //查找函数
updateScore(studentArr); //录入信息
}
/*
* updateScore函数用于录入学生成绩
* 传入一个对象组引用
* */
public static void updateScore(Student studentArr[]) {
for(int i = 0; i < 10; i++) {
studentArr[i].updateInfo(); //初始即赋值
}
}
/*
* search函数用于按学号查找学生成绩
* 传入一个对象组引用
* */
public static void search(Student studentArr[]) {
System.out.println("请输入要查询成绩的学生");
String id = input.next();
boolean isExits = false; // 是否查找成功标识
for(int i = 0; i < 10; i++) {
if(studentArr[i].getStudentStrId().equals(id)) {
studentArr[i].showStudentInfo();
isExits = true;
break;
}else {
continue;
}
}
if(!isExits) {
System.out.println("查无此人");
}
}
}