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


    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 + " 性别:" + gender + " 班级:" + grade + " 年龄:"
    + 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());
    }

    }

  • 相关阅读:
    eclipse-SDK-3.7-win32;eclipse-java-indigo-win32;eclipse-jee-indigo-win32 区别(ZZ)
    Marketplace Client- Download
    Log4J2基本配置
    Map 迭代 两种方法
    Python Argparse模块
    Python操作Memcached
    MySQL参数调优
    Nginx调优
    JavaScript知识点总结[部分]
    python optparser模块
  • 原文地址:https://www.cnblogs.com/Lovemeifyoudare/p/10302663.html
Copyright © 2020-2023  润新知