1 随机点名器 1
代码实现:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class shu20_1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("需要添加多少个学生");
int n = sc.nextInt();
sc.nextLine();
while (n-- > 0) {
System.out.println("输入学生姓名:");
String s = sc.nextLine();
list.add(s);
}
System.out.println(list);
Random rand = new Random();
int i = rand.nextInt(list.size());
String s = list.get(i);
System.out.println(s);
}
}
注意事项
2 随机点名器 2
代码实现:
public class Students {
private String name;
private String sex;
public Students(String name, String sex) {
this.name = name;
this.sex = sex;
}
public Students() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "姓名:"+name+" 性别:"+sex;
}
}
方法调用:
import itheima.shu20.s5.Students;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class shu20_2 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("这是一个学生点名系统");
System.out.println("请输入学生人数");
int a = input.nextInt();
input.nextLine();
//定义三个集合分别存储学生,男学生,女学生
ArrayList<Students> s = new ArrayList<>();
ArrayList<Students> s1 = new ArrayList<>();
ArrayList<Students> s2 = new ArrayList<>();
while (a-- > 0) {
Students students = new Students();
System.out.printf("输入姓名 %d号:", 3 - a);
students.setName(input.nextLine());
System.out.printf("输入性别 %d号:", 3 - a);
students.setSex(input.nextLine());
System.out.println();
s.add(students);
}
for (int i = 0; i < s.size(); i++) {
if (s.get(i).getSex().equals("男")) {
s1.add(s.get(i));
} else {
s2.add(s.get(i));
}
}
System.out.println(s);
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
Collections.shuffle(list);
Integer i = list.get(0);
Random r = new Random();
if (i == 0) {
int i2 = r.nextInt(s2.size());
Students students = s2.get(i2);
System.out.println("最后点了" + students);
} else {
int i1 = r.nextInt(s1.size());
Students students = s1.get(i1);
System.out.println("最后点了" + students + "同学");
}
}
}
3 随机点名器 3
代码实现:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class shu20_3 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("下面是一个点名器");
System.out.print("输入学生人数:");
int n = input.nextInt();
input.nextLine();
ArrayList<String> list0 = new ArrayList<>();
ArrayList<String> list1 = new ArrayList<>();
while (n-- > 0) {
list0.add(input.nextLine());
}
Random r = new Random();
while (!list0.isEmpty()) {
System.out.println("输入1 开始点名 \n输入2 退出");
if (input.nextInt() == 1) {
int i = r.nextInt(list0.size());
System.out.println("点到了" + list0.get(i));
System.out.println();
//原集合删除元素,另一个集合添加进去
list1.add(list0.get(i));
list0.remove(i);
//如果集合中的人全点完了,那就将两个集合互换进行下一轮操作
if (list0.isEmpty()) {
list0 = new ArrayList<>(list1);
list1 = new ArrayList<>(list0);
}
} else {
System.exit(0);
}
}
}
}
标签:Java,String,ArrayList,System,笔记,println,new,黑马,out
From: https://blog.csdn.net/2302_79847831/article/details/144222194