⽤java写⼀个简单的随机点名系统
⽤java写⼀个简单的随机点名系统
该系统总共有5个功能:
注:在你输⼊0退出系统之前,它会在1秒延时后继续显⽰菜单;延时时间以及初始化的学⽣姓名都可以⾃⾏修改
(在⽂末我会将这⼏个功能的运⾏截图展⽰出来)
⼤概抽象出三个类:
MyUtils.java ⼯具类,主要写了对数组的增删打印等功能和⽣成随机数等⽅法。当然你也可以⾃⼰调⽤系统⾃⼰包⾥的⽅法。StudentCrm.java 主类,主要是写对具体某学⽣的⼀些操作以及菜单的显⽰。
Student.java 学⽣类,学⽣的基本信息,也就是姓名咯。
import java.util.Random;
public class MyUtils {
//向数组中增加⼀个元素
public static String[] addArr(String[] arr, String num) {
String[] temp = new String[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
temp[i] = arr[i];
}
temp[arr.length] = num;
return temp;
}
//向数组中删除⼀个元素
public static String[] delArr(String[] arr, String str) {
String[] a = new String[arr.length-1];
for (int i = 0; i < arr.length-1; i++) {
if (arr[i].equals(str)) {
a[i] = arr[i+1];
}else{
a[i] = arr[i];
}
}
return a;
}
//获取⼀个num以内的随机数
public static int getRandom(int num){
Random random = new Random();
Int(num);
}
// 打印数组
public static String printArrays(String[] arr) {
java生成随机数的方法String str = "[";// 字符串开头
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
str += arr[i] + "]";
} else {
str += arr[i] + ", ";
}
}
return str;
}
}
import java.util.Scanner;
public class StudentCrm {
static boolean flag = false;//当flag为false的时候菜单显⽰不延时,反之延时 public static void main(String[] args) throws InterruptedException {
Student stu = new Student();
String msg = "请输⼊您想执⾏的操作:";
label:while(true){
menu();//调⽤菜单⽅法,显⽰菜单
Scanner scanner = new Scanner(System.in);
System.out.print(msg);
switch (Int()){
case 0:
System.out.println("退出系统");
break label;
case 1:
addStudent(stu);//增加学⽣
break;
case 2:
delStudent(stu);//删除学⽣
delStudent(stu);//删除学⽣
break;
case 3:
randStudent(stu);//随机打印⼀个学⽣
break ;
case 4:
printAll(stu);//打印所有的学⽣
break ;
default:
msg = "您输⼊有误,请重新输⼊:";
}
}
}
public static void addStudent(Student stu){
System.out.print("请输⼊您想增加的学⽣姓名:");
Scanner scanner = new Scanner(System.in);
String[] arr = MyUtils.Name() ,());
stu.setName(arr);
if(arr != null){
System.out.println("学⽣添加成功!");
}else {
System.out.println("学⽣添加失败!");
}
}
public static void delStudent(Student stu){
System.out.print("请输⼊您想删除的学⽣姓名:");
Scanner scanner = new Scanner(System.in);
String[] arr = MyUtils.Name(),());
stu.setName(arr);
if(arr != null){
System.out.println("学⽣删除成功!");
}else {
System.out.println("学⽣删除失败!");
}
}
public static void randStudent(Student stu){
int i = Name().length);
System.out.println("随机到的同学为:"+Name()[i]);
}
public static void printAll(Student stu){
System.out.println("全部学⽣为:\n"+MyUtils.Name())); }
public static void menu() throws InterruptedException {
if(flag){
Thread.sleep(1000);//当不是第⼀次显⽰菜单时,延时1秒钟
}
flag = true;
System.out.println("*********菜单**********");
System.out.println("(请输⼊对应功能前的序号)");
System.out.println("\t0.退出系统");
System.out.println("\t1.增加学⽣");
System.out.println("\t2.删除学⽣");
System.out.println("\t3.随机点名");
System.out.println("\t4.打印全部同学姓名");
System.out.println("***********************");
}
}
public class Student {
private String[] name = {"同学1","同学2","同学3","同学4","同学5","同学6","同学7","同学8","同学9", "同学10","同学11","同学12","同学13","同学14"};
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
}
运⾏截图:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论