Java创建一个学生类

创建一个学生类,每个学生有姓名属性、班级名称属性(String)、课程名称属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。(1... 创建一个学生类,每个学生有姓名属性、班级名称属性(String)、课程名称属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。
(1) 请列出所有学生的信息;
(2) 请打印class1班的总分和平均分;
(3) 请打印Java、C#课程的平均分。( 用ArrayList对象处理)
学生信息在测试类中自己构造。
姓名 班级名 课程名 考试成绩
张三 class1 Java 85
周乐儿 class1 C# 79
王涛 class2 C# 52
李明 class2 Java 48
展开
 我来答
流浪的小草0
2017-10-07 · 超过31用户采纳过TA的回答
知道答主
回答量:52
采纳率:0%
帮助的人:55.2万
展开全部

Student.java类:

public class Student {
    private String name;
    private String className;
    private String courseName;
    private int score;

    public Student(){

    }

    public Student(String name, String className, String courseName, int score) {
        this.name = name;
        this.className = className;
        this.courseName = courseName;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

main类:

import java.util.ArrayList;

public class StudentMain {
    public static void main(String[] args){
        //初始化学生信息
        Student student1 = new Student("张三", "class1", "Java", 85);
        Student student2 = new Student("周乐儿", "class1", "C#", 79);
        Student student3 = new Student("王涛", "class2", "C#", 52);
        Student student4 = new Student("李明", "class2", "Java", 48);
        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);

        //(1) 请列出所有学生的信息;
        System.out.println("(1) 请列出所有学生的信息;");
        System.out.println("姓名 班级名 课程名 考试成绩");
        for (Student student : students) {
            System.out.println(student.getName() + " " + student.getClassName() + " " + student.getCourseName() + " " +
            student.getScore());
        }

        //(2) 请打印class1班的总分和平均分;
        System.out.println("(2)请打印class1班的总分和平均分;");
        ArrayList<Integer> class1StudentScore = new ArrayList<>();
        int class1StudentTotalScore = 0;
        for (Student student : students) {
            if (student.getClassName().equals("class1")){
                class1StudentTotalScore += student.getScore();
                class1StudentScore.add(student.getScore());
            }
        }
        System.out.println("总分:" + class1StudentTotalScore);
        System.out.println("class1平均分:" + averageScore(class1StudentScore));

        //(3) 请打印Java、C#课程的平均分。
        System.out.println("(3) 请打印Java、C#课程的平均分。");
        ArrayList<Integer> javaStudentTotalScore = new ArrayList<>();
        ArrayList<Integer> cSharpStudentTotalScore = new ArrayList<>();

        for (Student student : students) {
            if (student.getCourseName().equals("Java")){
                javaStudentTotalScore.add(student.getScore());
            }

            if (student.getCourseName().equals("C#")){
                cSharpStudentTotalScore.add(student.getScore());
            }
        }
        System.out.println("java平均分:" + averageScore(javaStudentTotalScore));
        System.out.println("c#平均分:" + averageScore(cSharpStudentTotalScore));
    }

    //计算平均分
    private static double averageScore(ArrayList<Integer> list){
        double average = 0.0;
        for (int i = 0; i < list.size(); i++)  {
            average += list.get(i) ;
        }
        //考虑size为0的情况
        if (list.size() == 0){
            return 0.0;
        }
        return average/list.size();
    }

}

运行结果:

追问
大佬可以给我私信发一下完整代码么?我本人新手,手打老是出错,不知道该怎么改
追答
你给我一个邮箱,私信不能发文件
匿名用户
2017-10-07
展开全部

public class Test {

    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三", "class1", "Java", 85));
        studentList.add(new Student("周乐儿", "class1", "C#", 79));
        studentList.add(new Student("王涛", "class2", "C#", 52));
        studentList.add(new Student("李明", "class2", "Java", 48));
//===========================================================================
        System.out.println("姓名\t\t班级名\t\t课程名\t\t考试成绩");
        for (Student student : studentList) {
            System.out.println(student);
        }
//===========================================================================
        float class1SumScore = 0;
        int class1StudentCount = 0;
        for (Student student : studentList) {
            if (student.isClass1()) {
                class1SumScore += student.getScore();
                class1StudentCount++;
            }
        }
        System.out.println("\nclass1班的总分:" + class1SumScore + "\t平均分:" + class1SumScore / class1StudentCount);
//===========================================================================
        float javaScore = 0;
        float javaStudentCount = 0;
        float cSharpScore = 0;
        float cSharpStudentCount = 0;
        for (Student student : studentList) {
            if (student.isJava()) {
                javaScore += student.getScore();
                javaStudentCount++;
            } else if (student.isCSharp()) {
                cSharpScore += student.getScore();
                cSharpStudentCount++;
            }
        }
        System.out.println("\nJava课程的平均分:" + javaScore / javaStudentCount + "\tC#课程的平均分:" + cSharpScore / cSharpStudentCount);
    }
}

class Student {
    private String name;
    private String className;
    private String courseName;
    private int score;

    public Student(String name, String className, String courseName, int score) {
        this.name = name;
        this.className = className;
        this.courseName = courseName;
        this.score = score;
    }

    public boolean isClass1() {
        return "class1".equals(className);
    }

    public boolean isJava() {
        return "Java".equals(courseName);
    }

    public boolean isCSharp() {
        return "C#".equals(courseName);
    }

    public String getName() {
        return name;
    }

    public String getClassName() {
        return className;
    }

    public String getCourseName() {
        return courseName;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return name + "\t\t" + className + "\t\t" + courseName + "\t\t" + score;
    }
}

更多追问追答
追问
大佬直接把完整代码给我发来好嘛,截图照相都可以
追答
这个就是啊,运行不了吗?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
千叶育儿
2010-03-05 · 育儿、诗词分享,每天进步一点点!
千叶育儿
采纳数:1524 获赞数:5379

向TA提问 私信TA
展开全部
public class TestStu {

/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student("001","Jack",56,70,80);
Student s2 = new Student("007","Lucy",90,80,70);
float result1 = s1.getChinese()+s1.getEnglish()+s1.getMath();
float result2 = s2.getChinese()+s2.getEnglish()+s2.getMath();
if(result1>result2){
System.out.println(s1.toString());
}else{
System.out.println(s2.toString());
}
}

}

class Student{
private String id;
private String name;
private float chinese;
private float english;
private float math;

public Student(){}
public Student(String id,String name,float chinese,float english,float math){
this.id=id;
this.name=name;
this.chinese=chinese;
this.english=english;
this.math=math;
}

public float getChinese() {
return chinese;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "id:"+id+"\n name:"+"\n chinese:"+chinese+"\n english:"+english+"\n math:"+math;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wwwqvod
2010-03-05 · TA获得超过307个赞
知道小有建树答主
回答量:378
采纳率:0%
帮助的人:291万
展开全部
//学生类
public class Student{
private String stu_id;
private String stu_name;
private float math;
private float chinese;
private float computer;

public void setStu_id(String stu_id){
this.stu_id=stu_id;
}
public String getStu_id(){
return stu_id;
}
public void setStu_name(String stu_name){
this.stu_name=stu_name;
}
public String getStu_name(){
return stu_name;
}
public void setMath(float math){
this.math=math;
}
public float getMath(){
return math;
}
public void setChinese(float chinese){
this.chinese=chinese;
}
public float getChinese(){
return chinese;
}
public void setComputer(float computer){
this.computer=computer;
}
public float getComputer(){
return computer;
}

}

//主方法的类
public class Start{
public static void main(String[] args){
Student stu1=new Student();
stu1.setStu_id("No1");
stu1.setStu_name("张三");
stu1.setMath(89.5f);
stu1.setChinese(70f);
stu1.setComputer(98f);
Student stu2=new Student();
stu2.setStu_id("No2");
stu2.setStu_name("李四");
stu2.setMath(79.5f);
stu2.setChinese(90f);
stu2.setComputer(68f);
float stu1Sum = stu1.getMath()+stu1.getChinese()+stu1.getComputer();
float stu2Sum = stu2.getMath()+stu2.getChinese()+stu2.getComputer();
if(stu1Sum > stu2Sum){
System.out.println ("总分最高分为:"+stu1Sum);
System.out.println ("学号:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("数学:"+stu1.getMath());
System.out.println ("语文:"+stu1.getChinese());
System.out.println ("计算机:"+stu1.getComputer());
}else if(stu1Sum < stu2Sum){
System.out.println ("总分最高分为:"+stu2Sum);
System.out.println ("学号:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("数学:"+stu2.getMath());
System.out.println ("语文:"+stu2.getChinese());
System.out.println ("计算机:"+stu2.getComputer());
}else{
System.out.println ("总分一样高为:"+stu1Sum);
System.out.println ("学号:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("数学:"+stu1.getMath());
System.out.println ("语文:"+stu1.getChinese());
System.out.println ("计算机:"+stu1.getComputer());
System.out.println ("学号:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("数学:"+stu2.getMath());
System.out.println ("语文:"+stu2.getChinese());
System.out.println ("计算机:"+stu2.getComputer());
}
}
}

希望对你有帮助!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
unodos
2010-03-06 · TA获得超过134个赞
知道答主
回答量:184
采纳率:0%
帮助的人:54.1万
展开全部
主方法的类:
public class DriverStudent {
public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
float sum1,sum2;

s1.setId("001");
s1.setName("学生甲");
s1.setScore(70.5f,75.0f,87.0f);

s2.setId("002");
s2.setName("学生乙");
s2.setScore(89.5f,61.0f,92.0f);

sum1 = s1.getSum();
sum2 = s2.getSum();

if(sum1 > sum2){
System.out.println(s1.getInfo());
}else if(sum1 < sum2){
System.out.println(s2.getInfo());
}else{
System.out.println("The two students got same sum scores."+s1.getSum());
System.out.println(s1.getInfo());
System.out.println(s2.getInfo());
}
}
}
-----------------------------------------------------------------------
学生类

public class Student {
private String id;
private String name;
private int size = 3;
private float[] score;

public Student(){
size =3;
score = new float[size];
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float[] getScore() {
return score;
}

public void setScore(float a,float b,float c) {
score[0] = a;
score[1] = b;
score[2] = c;
}

public float getSum(){
float sum = 0;
for(int i = 0; i<size;i++){
sum += score[i];
}
return sum;
}

public String getInfo(){
return "ID = "+ id +" Name = " + name + " sum ="+ this.getSum();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式