使用面向对象思想,定义一个学生类Student,该类包括以下字段:姓名name,学号stuNo和成绩score。
使用面向对象思想,定义一个学生类Student,该类包括以下字段:姓名name,学号stuNo和成绩score。定义一个测试类TestStudent,在该类中定义main...
使用面向对象思想,定义一个学生类Student,该类包括以下字段:姓名name,学号stuNo和成绩score。定义一个测试类TestStudent,在该类中定义main入口方法,在main方法中初始化5个学生对象并存入数组,循环输出数组中的元素 Student类定义如第一张图,实现后效果为第二张图
展开
2个回答
展开全部
package com.test;
public class Student {
public String name;
public String stuNo;
public float score;
public Student(String name, String stuNo, float score) {
this.name = name;
this.stuNo = stuNo;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
public class TestStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=new Student("Jack","1001",67);
Student s2=new Student("Rose","1002",89);
Student s3=new Student("James","1003",45);
Student s4=new Student("Lucy","1004",95);
Student s5=new Student("Anna","1005",80);
Student [] sArr={s1,s2,s3,s4,s5};
System.out.println("姓名"+"\t"+"学号"+"\t"+"成绩");
for(int i=0;i<sArr.length;i++)
{
Student s=sArr[i];
System.out.println(s.getName()+"\t"+s.stuNo+"\t"+s.score);
}
}
}
展开全部
你好,纯手打,建议看一下是否符合你的要求。
public class Student {
private String name;
private Integer stuNo;
private Double score;
//省略get set方法
public Student(){
}
public Student(String name,Integer stuNo,Double score){
this.name = name;
this.stuNo = stuNo;
this.score = score;
}
public String toString(){
return this.name+"\t"+this.stuNo+"\t"+this.score;
}
}
public class TestStudent {
public static void main(String[] args){
List<Student> list = new ArrayList<Student>();
list.add(new Student("Jack",1001,67.0d));
list.add(new Student("Rose",1002,89.0d));
list.add(new Student("James",1003,45.0d));
list.add(new Student("Lucy",1004,95.0d));
list.add(new Student("Anna",1005,80.0d));
Systen.out.println("姓名\t学号\t成绩");
for(Student stu : list){
System.out.println(stu.toString());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询