一个简单的计算选修课程绩点的程序,欢迎大家指点下.
package 选修课程;/**
*
* @author Administrator
*/
public class Student { //学生类
Course s=new Course();
String stuName;
double maxGAP,minGAP;
static int stuID;
static int numberCourse;
static Course[] list =new Course[numberCourse];
int [] stuSecor=new int[numberCourse];
double stuGAP;
public Student()
{
list=new Course[numberCourse];
for(int i=0;i<numberCourse;i++)
list[i]=new Course();
stuSecor=new int[numberCourse];
}
public int CourseNum(int x)
{
numberCourse=x;
return numberCourse;
}
public String getstuName()
{
return stuName;
}
public double getGAP()
{
return stuGAP;
}
public static void setCourse( Course[] topic)
{
for(int i=0;i<topic.length;i++)
{
list[i].CourseName=topic[i].CourseName;
list[i].CourseMark=topic[i].CourseMark;
}
}
public void addstudent(String name,int ID,int[]secor)
{
stuName=name;
stuID=ID;
for(int i=0;i<secor.length;i++)
stuSecor[i]=secor[i];
}
public double getstuGAP()
{
int i=0;
double smark=0,sumGAP=0,summark=0;
for(i=0;i<stuSecor.length;i++)
{
if(stuSecor[i]>=95)
smark=4.5;
else if(stuSecor[i]>=90)
smark=4.0;
else if(stuSecor[i]>=85)
smark=3.5;
else if(stuSecor[i]>=80)
smark=3.0;
else if(stuSecor[i]>=75)
smark=2.5;
else if(stuSecor[i]>=70)
smark=2.0;
else if(stuSecor[i]>=65)
smark=1.5;
else if(stuSecor[i]>=60)
smark=1.0;
else
smark=0;
sumGAP+=smark*list[i].CourseMark;
summark+=list[i].CourseMark;
}
stuGAP=sumGAP/summark;
return stuGAP;
}
public void setswap(Student stu1,Student stu2)
{
String name;
int id;
double GAP;
int[] secor = new int[Student.numberCourse];
name=stu1.stuName;
id=stu1.stuID;
GAP=stu1.stuGAP;
for(int i=0;i<Student.numberCourse;i++)
secor=stu1.stuSecor;
stu1.stuName=stu2.stuName;
stu1.stuID=stu2.stuID;
stu1.stuGAP=stu2.stuGAP;
for(int i=0;i<Student.numberCourse;i++)
stu1.stuSecor=stu2.stuSecor;
stu2.stuGAP=GAP;
stu2.stuID=id;
stu2.stuName=name;
for(int i=0;i<Student.numberCourse;i++)
stu2.stuSecor=secor;
}
public void maxminGAP(Student[] stu)
{
double max,min;
if(stu.length==0)
min=max=-1;
min=max=stu[0].stuGAP;
for(int i=0;i<stu.length;i++)
{
if(max<stu[i].stuGAP)
max=stu[i].stuGAP;
if(min>stu[i].stuGAP)
min=stu[i].stuGAP;
}
maxGAP=max;
minGAP=min;
}
public double getmin()
{
return minGAP;
}
public double getmax()
{
return maxGAP;
}
public void SortGPA( Student[] stu)
{
int i, j, pos;
for(i=0;i<stu.length-1;i++)
{
for (j = (pos = i); j < stu.length; j++)
if (stu[pos].stuGAP< stu[j].stuGAP)
pos = j;
if (pos != i)
stu[i].setswap( stu[i], stu[pos]);
}
}
}
public class Course { \\ Course类
String CourseName;
int CourseMark;
public Course()
{
}
public Course(String Name,int Mark)
{
CourseName=Name;
CourseMark=Mark;
}
public String getCourseName()
{
return CourseName;
}
public int getCourseMark()
{
return CourseMark;
}
}
package 选修课程;
import java.util.*;
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Course r=new Course();
Student t=new Student();
int Num, Mark,Id;
String Name;
Scanner in=new Scanner(System.in);
System.out.println("请输入学生总人数");
Num=in.nextInt();
System.out.println("请输入选修课程总数");
int k=in.nextInt();
k=t.CourseNum(k);
Student [] stu=new Student[Num];
for(int i=0;i<Num;i++)
stu[i]=new Student();
Course [] tp =new Course [k];
int [] score =new int [k];
for(int i=0;i<k;i++)
{
System.out.println("请输入选修课程名称");
Name=in.next();
System.out.println("请输入选修课程学分");
Mark=in.nextInt();
tp[i]=new Course(Name,Mark);
}
Student.setCourse(tp);
for(int i=0;i<Num;i++)
{
System.out.println("请输入学生姓名") ;
Name=in.next();
System.out.println("请输入学生学号");
Id=in.nextInt();
for(int j=0;j<k;j++)
{
System.out.println("请输入学生"+tp[j].getCourseName()+"的成绩");
score[j]=in.nextInt();
}
stu[i].addstudent(Name, Id, score);
t.getstuGAP();
System.out.println("您的GPA值是"+stu[i].getstuGAP());
}
t.maxminGAP(stu);
System.out.println("最大GAP是" + t.getmax()+"最小GAP是"+t.getmin());
t.SortGPA(stu);
for(int i=0;i<Num;i++)
System.out.println("从高到低是"+stu[i].getGAP());
}
}