通过两个类来现实此功能:
- 创建一个学生类Student,用来存储学生信息和成绩。
- 创建一个调用类Student_Method,用来打印学生信息和成绩。
Student类代码如下:
public class Student {
private String name;
private String Sno;
private int English_Score;
private int Math_Score;
private int Sports_Score;
Student(String name,String Sno,int English_Score,int Math_Score,int Sports_Score){
this.name = name;
this.Sno = Sno;
this.English_Score = English_Score;
this.Math_Score = Math_Score;
this.Sports_Score = Sports_Score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public int getEnglish_Score() {
return English_Score;
}
public void setEnglish_Score(int english_Score) {
English_Score = english_Score;
}
public int getMath_Score() {
return Math_Score;
}
public void setMath_Score(int math_Score) {
Math_Score = math_Score;
}
public int getSports_Score() {
return Sports_Score;
}
public void setSports_Score(int sports_Score) {
Sports_Score = sports_Score;
}
public String toString(){
System.out.println(name+"信息如下:");
return "名字 :" + name + ", 学号 :" + Sno + ", 英语成绩 :" + English_Score + ", 数学成绩 :" + Math_Score
+ ", 体育成绩 :" + Sports_Score + "";
}
public static void main(String[] args) {
}
}
Student_Method类代码如下:
public class Student_Method {
static Student[] stu;
public static void MethodOne(){
//Scanner打印学生信息
Scanner scan=new Scanner(System.in);
System.out.println("请输入要查找名字:");
String ps=scan.next();
//判断查询成绩
/*if(person.matches(".*张.*")){
System.out.println(arr[0]);
System.out.println(arr[3]);
}*/
if(ps.contains("张")){ //ps.contains("张") 模糊查询,有张字的对象
System.out.println(stu[0]);
System.out.println(stu[4]);
}else if(ps.contains("李")){
System.out.println(stu[1]);
}else if(ps.contains("王")){
System.out.println(stu[2]);
}else if(ps.contains("赵")){
System.out.println(stu[3]);
}
System.out.println();
}
public static void MethodTwo(){
//Scanner打印不及格人数
Scanner scan1=new Scanner(System.in);
System.out.println("请输入要查找不及格科目:1、英语成绩;2、数学成绩;3、体育成绩");
int ps1=scan1.nextInt();
int MathScorecount=0;
int EnglishScorecount=0;
int SportScorecount=0;
if(ps1 == 1){
System.out.print("英语成绩不及格的有:");
for(int i=0;i<=stu.length-1;i++){
if(stu[i].getEnglish_Score()<=60){
System.out.print(stu[i].getName()+",");
EnglishScorecount++;
}
}
System.out.println("总共有:"+EnglishScorecount+"人");
}else if(ps1 == 2){
System.out.print("数学成绩不及格的有:");
for(int i=0;i<=stu.length-1;i++){
if(stu[i].getMath_Score()<=60){
MathScorecount++;
System.out.print(stu[i].getName()+",");
}
}
System.out.println("总共有:"+MathScorecount+"人");
}else if(ps1 == 3){
System.out.print("体育成绩不及格的有:");
for(int i=0;i<=stu.length-1;i++){
if(stu[i].getSports_Score()<=60){
System.out.print(stu[i].getName()+",");
SportScorecount++;
}
}
System.out.println("总共有:"+SportScorecount+"人");
}
}
public static void main(String[] args) {
Student_Method sm = new Student_Method();
//引用数组 存储学生对象
stu=new Student[5];
stu[0]=new Student("张三","117330",48,57,82);
stu[1]=new Student("李四","117331",56,70,80);
stu[2]=new Student("王五","117332",44,68,78);
stu[3]=new Student("赵六","117333",88,74,56);
stu[4]=new Student("张三丰","117334",92,82,52);
sm.MethodOne();
sm.MethodTwo();
}
}