Java定义学生类student
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java定义学生类student相关的知识,希望对你有一定的参考价值。
增加删除单个学生 成批增加 按名字查找学生,显示结果信息,显示学生信息 增加删除班级
import java.util.*;class Student
//这里为了方便赋值,减少代码,我就用public了。
public int id;
public String name;
public String banji;
//重写构造器,方便增加学生
public Student(int id, String name, String banji)
this.id = id;
this.name = name;
this.banji = banji;
public int getId()
return id;
//输出学生信息
public String toString()
System.out.println("学号:"+id);
System.out.println("姓名:"+name);
System.out.println("班级:"+banji);
public class Main
public static void main(String[] args)
//增加单个学生
Student s1 = new Student(1,"小童鞋_成er","1班");
//输出学生信息
System.out.println(s1);
//批量增加学生,弄一个ArrayList
ArrayList<Student> list = new ArrayList<Student>();
//比如插入10个
for(int i = 1; i <= 10; i++)
list.add(new Student(i,"学生"+i,i+"班"));
//显示学生
for(Iterator<Student> it = list.iterator(); it.hasNext();)
System.out.println(it.next());
//删除学生用ListIterator来弄
for(ListIterator<Student> lli = list.listIterator(); lli.hasNext();)
//比如要删除学号为1的学生
if(lli.next().getId()==1)
lli.remove();
//最后便利这个ListIterator就可以了
追问
错误: 找不到或无法加载主类 student
追答你的文明名要Main.java
参考技术A public class Studentint id;
String name;
参考技术B class
student
string
studid;
string
classid;
string
name;
string
sex;
int
age;
public
student()//无参构造方法
public
student(string
studid,string
classid,string
name,string
sex,int
age)//有参构造方法
this.studid=studid;
this.classid=classid;
this.name=name;
this.sex=sex;
this.age=age;
//获得属性方法,一般情况set和get都会有,因因为用构造函数初始化属性不太常见,一般用set方法,get和set方法可以用eclipse的自动生成工具生成,tostring也可以,所以只要写好属性其他方法都可以自动生成,
public
string
getstudid()
return
studid;
public
string
getclassid()
return
classid;
public
string
getname()
return
name;
public
string
getsex()
return
sex;
public
int
getstudid()
return
age;
public
string
tostring()
return
this.studid+";"+this.classid+";"+this.name+";"+this.sex+";"+this.age;
class
test
public
static
void
main(string[]
args)
student
s=new
student("01455","0902","彬彬有礼","男",25);
system.out.println(s.tostring());
参考技术C 这些功能都差不多可以做一个毕业论文了
简单来说:
增加就是insert : insert into student (id,name) values(1,'thename');
删除就是delete: delete from student where id=1;
成批增加就是addbatch
按名字查找学生就是lick: select * from student where name licke '%the%' 参考技术D Class Student
private String id;
private String num;//学号
private String name;//姓名
private String age;//年龄
public void setId(String id)
this.id = id;
public String getId()
return this.id;
public void setNum(String num)
this.num = num;
public String getNum()
return this.num ;
public void setName(String name)
this.name = name;
public String getNamed()
return this.name;
public void setAge(String iage)
this.age = age;
public String getAge()
return this.age;
构建一个学生Student,根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。(代码
定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;
sName表示姓名;
sSex表示性别;
sAge表示年龄;
sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
方法一:
package com.Summer_0511.cn; import java.util.Arrays; class Student{ private int sNo; private String sName; private String sSex; private int sAge; private int sJava; public Student(int sNo, String sName, String sSex, int sAge, int sJava) { super(); this.sNo = sNo; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sJava = sJava; } public int getsNo() { return sNo; } public String getsName() { return sName; } public String getsSex() { return sSex; } public int getsAge() { return sAge; } public int getsJava() { return sJava; } @Override public String toString() { return "student [sNo=" + sNo + ", sName=" + sName + ", sSex=" + sSex + ", sAge=" + sAge + ", sJava=" + sJava + "]"; } } public class Test05 { public static void main(String[] args) { Student s1 = new Student(1, "小a","男" , 18, 95); Student s2 = new Student(2,"小b","女",20,90); Student s3 = new Student(3,"小c","女",21,65); Student s4 = new Student(4, "小d", "男", 18, 100); Student s5 = new Student(5, "小e", "男", 15, 85); int [] s = new int[5]; s[0] = s1.getsJava(); s[1] = s2.getsJava(); s[2] = s3.getsJava(); s[3] = s4.getsJava(); s[4] = s5.getsJava(); int sum = 0; int Max=s[1],Min=s[1]; for (int i = 0; i < s.length; i++) { sum = sum+s[i]; if (Max < s[i]) { Max = s[i]; } if (Min >s[i]) { Min = s[i]; } } System.out.println("java的平均成绩为"+sum/5); System.out.println("java成绩的最小值"+Min); System.out.println("java成绩的最大值"+Max); } }
方法二:
package stage3; class Student{ private int sNo; private String sName; private String sSex; private int sAge; private int sJava; public Student(int sNo, String sName, String sSex, int sAge, int sJava) { this.sNo = sNo; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sJava = sJava; } public int getsNo() { return sNo; } public void setsNo(int sNo) { this.sNo = sNo; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsSex() { return sSex; } public void setsSex(String sSex) { this.sSex = sSex; } public int getsAge() { return sAge; } public void setsAge(int sAge) { this.sAge = sAge; } public int getsJava() { return sJava; } public void setsJava(int sJava) { this.sJava = sJava; } } public class Test4 { public static void main(String[] args) { // 学员信息 Student stu1 = new Student(1, "郭靖", "男", 22, 90); Student stu2 = new Student(2, "欧阳锋", "男", 61, 91); Student stu3 = new Student(3, "杨康", "男", 20, 92); Student stu4 = new Student(4, "欧阳克", "男", 23, 93); Student stu5 = new Student(5, "穆念慈", "女", 24, 94); Student [] stus = new Student[5]; stus[0] = stu1; stus[1] = stu2; stus[2] = stu3; stus[3] = stu4; stus[4] = stu5; System.out.println("---信息---"); for(int i = 0; i<stus.length;i++){ System.out.println(stus[i].getsNo()+"\t" +stus[i].getsName() +"\t"+stus[i].getsSex() +"\t"+stus[i].getsAge() +"\t"+stus[i].getsJava()); } int sum = 0; for(int i = 0; i<stus.length;i++){ sum += stus[i].getsJava(); } System.out.println("平均成绩:"+(sum/5)); } }
以上是关于Java定义学生类student的主要内容,如果未能解决你的问题,请参考以下文章