Java创建一个学生类

创建一个学生类,每个学生有姓名属性、班级名称属性(String)、课程名称属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。
(1) 请列出所有学生的信息;
(2) 请打印class1班的总分和平均分;
(3) 请打印Java、C#课程的平均分。( 用ArrayList对象处理)
学生信息在测试类中自己构造。
姓名 班级名 课程名 考试成绩
张三 class1 Java 85
周乐儿 class1 C# 79
王涛 class2 C# 52
李明 class2 Java 48

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();
    }

}

运行结果:

追问

大佬可以给我私信发一下完整代码么?我本人新手,手打老是出错,不知道该怎么改

追答

你给我一个邮箱,私信不能发文件

温馨提示:内容为网友见解,仅供参考
第1个回答  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;
    }
}

追问

大佬直接把完整代码给我发来好嘛,截图照相都可以

追答

这个就是啊,运行不了吗?

追问

不可以的,老是提示我出错,我新手都不知道怎么改

追答

报的是什么错,截个图给我

第2个回答  2010-03-05
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;
}
}
第3个回答  2010-03-05
//学生类
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());
}
}
}

希望对你有帮助!
第4个回答  2010-03-06
主方法的类:
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();
}
}

用java表示学生信息 问题:用java怎么写学生表示?
getSex():获得性别;getAge()获得年龄;getJava():获得Java 课程成绩 (4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。*\/ class student{ String sNO,sName,sSex;int sAge;d...

如何使用java来创建一个学生类?
首先,你需要创建一个学生类来表示学生的信息,例如:public class Student { private String name;private int age;private String gender;private String grade;public Student(String name, int age, String gender, String grade) { this.name = name;this.age = age;this.gender = gender;this.g...

用JAVA编写一个学生类Student的程序
学生类:public class Student { private String num;\/\/学号 private String name;\/\/姓名 private String sex;\/\/性别 private int age;\/\/年龄 public Student(String num,String name,String sex,int age){ this.num = num;this.name = name;this.sex = sex;this.age = age;} public String ...

Java程序设计,定义一个表示学生的类Student
public class Student { \/\/定义一个学生类 private int StuNum; \/\/学号 private int Class; \/\/班级 private char Gender; \/\/性别 private int Age; \/\/年龄 public Student(int StuNum, int Class, char Gender, int Age){\/\/构造函数 this.stuNum = StuNum;this.class = Cla...

用java定义学生类(学号、姓名、成绩)。用列表list存放班级学生信息...
import java.util.Collections;import java.util.Comparator;import java.util.List;public class Admin { public static void main(String... args) { List data = new ArrayList();Student s0 = new Student();s0.setNo("000");s0.setName("a");s0.setChengji(100.0);data.add(s0);Stu...

用JAVA定义一个学生类Student来表示学生信息,学生类中包含成员有学号...
}public int getWai() {return wai;}public void setWai(int wai) {this.wai = wai;} Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最...

JAVA 定义一个学生类(Student),包含学号、姓名、年龄(7-60岁)三个私 ...
import java.util.Scanner;class Student{ private String num;private int age;private String name;public Student(String b,String a,int c) { num=b;name=a;age=c;} public int reage() { return age;} void shou() { if(age>=7&&age<=60)System.out.println(name+" "+num+" "+...

java定义一个学生类要求学号,姓名,年龄,三个成员变量
class Student{private int STU_NUM;private String STU_NAME;private int STU_AGE;public Student(int NUM,String NAME){this.STU_NAME=NAME;this.STU_NUM=NUM;}public Student(int NUM,String NAME,int AGE){this.STU_NAME=NAME;this.STU_NUM=NUM;this.STU_AGE=AGE;}} ...

在java中编写程序,定义一个学生类
编写一个Java应用程序,该程序包括3个类:定义一个学生类,Student有姓名,学号,选学的课程列表 定义一个课程类:课程名称,课程分数 (1)初始化一个学生,选择了三门课程,并添加到学生的属性中 (2)实现统计学生的总分功能 1.正确编写2个类(5分)2.统计学生的总分功能(5分)\/ public class ...

java 面像对象中, 定义一个学生类,可以创建五个学生对象,自动产生_百度...
\/\/第一种自动产生学号方法:用for循环,从一开始,一一放进去对象学生学号那里 StudentTest[] student=new StudentTest[5];\/\/五个对象数组 for(int i=0;i<student.length;i++){ student[i]=new StudentTest();\/\/new对象 student[i].setNumber(i+1);\/\/循环放入学号,System.out.println(...

相似回答