欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

随机点名测试(随机数 类)

程序员文章站 2023-03-31 23:42:48
package lianxi; public class Student { //学生类 private String name; private String gender; private String grade; private int age; public String getName( ......

package lianxi;

public class student { //学生类
private string name;
private string gender;
private string grade;
private int age;

public string getname() {
return name;
}

public string getgender() {
return gender;
}

public string getgrade() {
return grade;
}

public int getage() {
return age;
}

public void setname(string name) {
this.name = name;
}

public void setgender(string gender) {
this.gender = gender;
}

public void setgrade(string grade) {
this.grade = grade;
}

public void setage(int age) {
this.age = age;
}

public string tostring() {
return "姓名:" + name + "\t性别:" + gender + "\t班级:" + grade + "\t年龄:"
+ age;
}

}

//学生列表(添加学生 随机点名..)

package lianxi;

import java.util.arraylist;
import java.util.scanner;

public class studentlist {
arraylist<student> stlt = new arraylist<student>();

public void addstudent() {
scanner sc = new scanner(system.in);
for (int i = 1; i < 3; i++) {
student st = new student();
system.out.println("添加第" + i + "个学生姓名:");
st.setname(sc.next());
system.out.println("添加第" + i + "个学生性别:");
st.setgender(sc.next());
system.out.println("添加第" + i + "个学生班级:");
st.setgrade(sc.next());
system.out.println("添加第" + i + "个学生年龄:");
st.setage(sc.nextint());
stlt.add(st);
}
}

public void deletestudent(int tr) {
stlt.remove(tr);
}

public arraylist<student> printname() {
return stlt;
}

public string callname() {
/*
* math.random() 方法生成[0,1)范围内的double类型随机数;
* random类中的nextxxxx系列方法生成[0-n)的随机数; math.random() 线程安全,多线程环境能被调用;
* 如无特殊需求,则使用(int)(math.random()*n)的方式生成随机数即可。
*/
return stlt.get((int) (math.random() * stlt.size())).getname()
+ stlt.get((int) (math.random() * stlt.size())).getgender();// 此处学生的每个信息都是随机
// int index = new random().nextint(stlt.size());
// return stlt.get(index);
}
}

//测试

package lianxi;

public class maincallname {
public static void main(string[] args) {
studentlist sl = new studentlist();
sl.addstudent();
system.out.println(sl.printname());
system.out.println(sl.callname());
sl.deletestudent(1);
system.out.println(sl.printname());
system.out.println(sl.callname());
}

}