public class Test {
protected static boolean end_flag = true
public static void main(String[] args) {
Students student = new Students()
student.InputData()// 输入学生数据
String order
while (end_flag) {
System.out.println("请输入数字1-5:")
Scanner s = new Scanner(System.in)
order = s.nextLine()
student.Operator(order)
}
}
}
Students类:
import 蚂和java.util.ArrayListimport java.util.Scanner
public class Students {
private int STUDENTS_NUM = 5 // 学生的人数
private int SUBJECTS_NUM = 3 // 课程数
private String[][] data = new String[STUDENTS_NUM][SUBJECTS_NUM + 2]
private String name
private int Chinese_score
private int Math_score
private int English_score
public void SearchHighestScore(String[][] original) { // 查找最高成绩
int flag = Integer.parseInt(original[0][4]), position = 0
for (int i = 1 i < original.length i++) {
if (Integer.parseInt(original[i][4]) > flag) {
flag = Integer.parseInt(original[i][4])
position = i
}
}
System.out.print("最高分同学的姓名和总分为:")
System.out.println(original[position][0] + " " + original[position][4])
}
public void SearchLowestScore(String[][] original) {
int flag = Integer.parseInt(original[0][4]), position = 0
for (int i = 1 i < original.length i++) {
if (Integer.parseInt(original[i][4]) < flag) {
flag = Integer.parseInt(original[i][4])
position = i
}
}
System.out.print("最低分同学的姓名和总分为:")
System.out.println(original[position][0] + " " + original[position][4])
}
public void OrderStudents(String[][] original) {
String[][] result = original
String temp
for (int i = 1 i < result.length i++) {
for (int j = 尺让0 j < result.length - i j++) {
if (Integer.parseInt(result[j][4]) < Integer
.parseInt(result[j + 1][4])) {
temp = result[j][4]
result[j][4] = result[j 陵物局+ 1][4]
result[j + 1][4] = temp
temp = result[j][0]
result[j][0] = result[j + 1][0]
result[j + 1][0] = temp
}
}
}
System.out.println("排名\t" + "姓名\t" + "总分\t")
for (int i = 0 i < STUDENTS_NUM i++) {
System.out.println(i + 1 + "\t" + result[i][0] + "\t"
+ result[i][4])
}
}
public void ScoreUnder60(String[][] original) {
System.out.println("平均分在60以下的学生的姓名为:")
ArrayList temp = new ArrayList()
for (int i = 0 i < original.length i++) {
if (Integer.parseInt(original[i][4]) / SUBJECTS_NUM < 60) {
temp.add(original[i][0])
}
}
System.out.print(temp + " ")
System.out.println("\n" + "平均分在60以下的学生的人数为:"
+ temp.size())
}
public Integer SearchScore(String student_name) {
int position = -1
for (int i = 0 i < data.length i++) {
if (student_name.equals(data[i][0])) {
position = i
}
}
if (position != -1)
return Integer.valueOf(data[position][4])
else
return null
}
public void Operator(String order) {
if ("1".equals(order)) {
SearchHighestScore(data)
SearchLowestScore(data)
} else if ("2".equals(order)) {
OrderStudents(data)
} else if ("3".equals(order)) {
ScoreUnder60(data)
} else if ("4".equals(order)) {
String name
System.out.println("请输入学生的姓名:")
Scanner s = new Scanner(System.in)
name = s.nextLine()
if (SearchScore(name) != null)
System.out.println(name + "的总分为:" + SearchScore(name))
else
System.out.println("查无此人")
} else if ("5".equals(order)) {
Test obj = new Test()
obj.end_flag = false
System.out.println("退出")
} else {
System.out.println("输入有误!")
}
}
public void InputData() {
for (int i = 1 i <= STUDENTS_NUM i++) {
System.out.print("请输入第" + i + "个人的姓名" + ":")
Scanner s1 = new Scanner(System.in)
name = s1.nextLine()
data[i - 1][0] = name
System.out.print("请输入第" + i + "个人的语文成绩" + ":")
Scanner s2 = new Scanner(System.in)
Chinese_score = s2.nextInt()
data[i - 1][1] = String.valueOf(Chinese_score)
System.out.print("请输入第" + i + "个人的数学成绩" + ":")
Scanner s3 = new Scanner(System.in)
Math_score = s3.nextInt()
data[i - 1][2] = String.valueOf(Math_score)
System.out.print("请输入第" + i + "个人的英语成绩" + ":")
Scanner s4 = new Scanner(System.in)
English_score = s4.nextInt()
data[i - 1][3] = String.valueOf(English_score)
data[i - 1][4] = String.valueOf(Chinese_score + Math_score
+ English_score)
System.out.println(data[i - 1][4])
}
}
}
在类中声明一个静态变量如下 :static int num=0,每次类初始化将num 加一。
静态变量为所有类共享,num的大小即空册猛为斗桥创建的student对象的数目
程序稍微改动如姿态下:
class student {
String name
static int num =0
String id
float ywscore
float sxscore
float sumscore
float avgscore
student(String name,String id,float ywscore,float sxscore){
this.name=name
this.id=id
this.ywscore=ywscore
this.sxscore=sxscore
num ++
}
float sum(){
sumscore=sxscore+ywscore
return sumscore
}
float avg(){
avgscore=(sxscore+ywscore)/2
return avgscore
}
int getNum()
{
return num
}
void showinfo(){
System.out.println("name: "+name)
System.out.println("id: "+id)
System.out.println("mathscore: "+sxscore)
System.out.println("langue: "+ywscore)
System.out.println("avg: "+avg())
System.out.println("sum: "+sum())
}
}
//梁樱锋/: Filename: Teacher.java//老师类
public class Teacher {
//根据学生成绩的平均分由高到低排序
private void sortStudents(Student[] s){
Student t
for(int i=0i<s.length-1i++)
if(s[i+1].getAvgResult()>s[i].getAvgResult()){
t=s[i]
s[i]=s[i+1]
s[i+1]=t
i-=i==0?1:2
}
}
//公布学生考试成绩
private void printInfo(Student[] s){
for(int i=0i<s.lengthi++)
System.out.println(s[i].getCls()+": "+s[i].getName()+" 同学,\n总分: "+s[i].getTotalResult()+", 平均分:"+s[i].getAvgResult()+" " +
" 名列本次考试的第 "+(i+1)+" 名." +
"\n\t数学: "+s[i].getResult()[0]+" 分" +
"\n\t语文: "+s[i].getResult()[1]+" 分"+
"\n\t体育: "+s[i].getResult()[2]+" 分"+"\n")
}
public static void main(String[] args) {
String cls = "三(6)班"
Student 张三 = new Student("张三",cls,new float[]{80,70,90,})
Student 李四 = new Student("李四",cls,new float[]{90,75,65,})
Student 王五 = new Student("王五",cls,new float[]{85,95,70,})
Student[] all = {张三,李四,王五,}//全部学生放入一个数组,以便分析
Teacher th = new Teacher()//创建老师颂键类
th.sortStudents(all)//老师对学生进橡晌行排序
th.printInfo(all)//老师公布学生的结果
}
}
//学生类
class Student{
Student(){}
Student(String name,String cls,float[] r){
this.setName(name)
this.setCls(cls)
this.setResult(r)
}
private String name//姓名
private String cls//班级
private float[] results//成绩
//SETTER
public void setName(String s){name=s}
public void setCls(String s){cls=s}
public void setResult(float[] f){results=f}
//GETTER
public String getName(){return name}
public String getCls(){return cls}
public float[] getResult(){return results}
public float getTotalResult(){//获取学生总成绩
float f = 0
for(int i=0i<results.lengthi++)
f+=results[i]
return f
}
public float getAvgResult(){//获取学生平均成绩
return getTotalResult()/results.length
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)