zoukankan      html  css  js  c++  java
  • 随机点名

    随机点名

    package lijun.cn.demo1;

    public class Student {
    //描述学生 属性 姓名 年龄
    String name;
    int age;
    }

    package lijun.cn.demo1;

    import java.util.ArrayList;
    import java.util.Random;

    public class CallName {
    public static void main(String[] args) {
    //1 定义集合 存储的是Student类型
    ArrayList<Student> array= new ArrayList<Student>();
    // 2 调用添加的方法
    add(array);
    //3 遍历集合
    printArrayList(array);
    //4 随机点名
    randomStudentNmae(array);
    }
    //4.1 随机点名
    public static void randomStudentNmae(ArrayList<Student> array){
    Random r = new Random();
    //生成随机数的范围
    int num = r.nextInt(array.size());
    //随机数num 当成array集合的索引 从而获得名字和年龄
    Student s = array.get(num);
    System.out.println("=============");
    System.out.println(s.name+" "+s.age);
    }
    //3.1遍历集合
    public static void printArrayList(ArrayList<Student> array){
    for (int i = 0; i < array.size(); i++) {
    Student s = array.get(i);
    System.out.println(s.name+" "+s.age);
    }
    }
    // 2 .1书写添加的方法
    public static void add(ArrayList<Student> array){
    Student s1 = new Student();
    s1.name="张无忌";
    s1.age=22;
    Student s2 = new Student();
    s2.name="无双";
    s2.age=21;
    Student s3 = new Student();
    s3.name="建宁";
    s3.age=20;
    Student s4 = new Student();
    s4.name="龙儿";
    s4.age=25;
    //将4个人的名字和年龄存储到集合中
    array.add(s1);
    array.add(s2);
    array.add(s3);
    array.add(s4);
    }

    }

  • 相关阅读:
    背包问题
    计蒜客lev3
    线段树BIT操作总结
    图论题收集
    Codeforces Round #607 (Div. 2) 训练总结及A-F题解
    2-sat 学习笔记
    洛谷 P3338 【ZJOI2014】力/BZOJ 3527 力 题解
    $noi.ac$ #51 array 题解
    洛谷 P3292 【SCOI2016】幸运数字/BZOJ 4568 幸运数字 题解
    洛谷 P5283 【十二省联考2019】异或粽子 题解
  • 原文地址:https://www.cnblogs.com/lxy4/p/10522942.html
Copyright © 2011-2022 走看看