1、成绩单类、班级类、学生类、课程类。
其中成绩单中有其它类对象,班级类中有学生对象,学生类中有课程对象。分别用Vector<>或者ArrayList存放对象。
2、序列化所有对象保存到文件中。但是这样只能从从程序中存入文件,打开文件则是乱码显示。所以还是用数据库比较好。建立4张表,存放相应的信息,再对其进行SQL *** 作。
3、就是对数据一系列查询、插入、修改和删除 *** 作。建议用Vector<>存放对象 *** 作比较方便。
//学生类public class Student {
private String NO
private String name
private int math
private int physics
private int english
private int total = 0
public Student(String NO, String name, int math, int physics, int english) {
this.NO = NO
this.name = name
this.math = math
this.physics = physics
this.english = english
total = math + physics + english
}
public String getNO() {
return NO
}
public void setNO(String nO) {
NO = nO
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public int getMath() {
return math
}
public void setMath(int math) {
this.math = math
}
public int getPhysics() {
return physics
}
public void setPhysics(int physics) {
this.physics = physics
}
public int getEnglish() {
return english
}
public void setEnglish(int english) {
this.english = english
}
public int getTotal() {
return total
}
public void setTotal(int total) {
this.total = total
}
@Override
public String toString() {
return "[学号=" + NO + ", 姓名=" + name + ", 数学成绩=" + math + ", 物理成绩=" + physics + ", 英语="
+ english + ", 总分=" + total + "]"
}
}
//排序
public static class CompareStudent implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getTotal() < o2.getTotal() ? 1 : o1.getTotal() == o2.getTotal() ? 0 : -1
}
}
public class Test {
public static void main(String[] args) {
String filename = "res/student.txt"
System.out.println("读取" + filename + "文件的数据")
List<Student> result = readStudnetInfoFromFile(filename)
//排序
result.sort(new CompareStudent())
System.out.println("排序输出")
for (Student student : result) {
System.out.println(student)
}
System.out.println()
//不及格
System.out.println("数学不及格")
int nums = 0
for (Student student : result) {
if(student.getMath() < 60) {
System.out.println(student.getName() + " 数学成绩不及格")
nums ++
}
}
System.out.println("数学不及格总共有" + nums + "人")
}
//从文件中读取数据
private static List<Student> readStudnetInfoFromFile(String filename) {
File file = new File(filename)
if (!file.exists()) {
return null
}
List<Student> result = new ArrayList<>()
BufferedReader br = null
FileReader fr = null
try {
fr = new FileReader(file)
br = new BufferedReader(fr)
String line = null
while ((line = br.readLine()) != null) {
String[] infos = line.split("_")
Student stu = new Student(infos[0], infos[1], Integer.parseInt(infos[2]), Integer.parseInt(infos[3]),
Integer.parseInt(infos[4]))
result.add(stu)
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
if (br != null) {
br.close()
}
if(fr != null) {
fr.close()
}
} catch (IOException e) {
e.printStackTrace()
}
}
return result
}
//数据文件 注意文件路径
1_张三_60_70_80
2_李四_58_58_73
3_王二_63_75_85
4_莉莉_46_75_88
5_小明_85_76_56
import java.util.*import java.lang.*
public class GradeAnalysis{
Scanner student
String[]name
Double[]grades
double average=0
public GradeAnalysis()
{
student=new Scanner(System.in)
name=new String[10]
grades=new Double[10]
for(int i=0i<10i++)
{
System.out.println("请输入"+(i+1)+"号同学的姓名")
name[i]=student.next()
System.out.println("请输入"+(i+1)+"号同学的得分")
grades[i]=student.nextDouble()
}
StatisticAndPrint()
}
public void StatisticAndPrint(){
double A,B,C,D,E
double a,b,c,d,e
A=0B=0C=0D=0E=0
for(int i=0i<10i++)
{int grade=(int)(grades[i]/10)
switch(grade){
case 10:A++break
case 9: A++break
case 8: B++break
case 7: C++break
case 6:D++break
case 5: E++break
case 4: E++break
case 3: E++break
case 2: E++break
case 1: E++break
case 0: E++break
default:System.out.println("统计错误")break
}
}
a=A/10b=B/10c=C/10d=D/10e=E/10
for(int i=0i<10i++)
{average+=grades[i]}
average=average/10
System.out.println("\t\t"+"考试成绩统计情况分析"+"\n姓名:\t\t得分:\n")
for(int i=0i<10i++)
{ System.out.println(name[i]+"\t\t"+grades[i])}
System.out.println("\n本次平均分为"+average+"\n\t\t各等地情况\nA\tB\tC\tD\tE\n"+A+"\t"+B+"\t"+C+"\t"+D+"\t"+E+"\n所占人数百分数\n"+100*a+"%\t"+100*b+"%\t"+100*c+"%\t"+100*d+"%\t"+100*e+"%")
}
public static void main(String[]args)
{
new GradeAnalysis()
}
}
被当成参考答案的作业,嘿嘿,格式神马的绝对漂亮。你想改ABCDE的分数设定你就在switch里面改把,又不会的告诉我
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)