随机点名系统(去除重复)

// An highlighted block
package 随机点名小程序;

import java.util.Arrays;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		System.out.println("------欢迎使用本程序,坑人点名开始了-------");
		System.out.println("xxx班级:");
		Scanner sc = new Scanner(System.in);
		int n = 1; // 控制轮数
		while (true) {
			String[] arr = new String[] { "张三", "李四", "王五", "李青", "鲁班", "劫",
					"小巧", "周瑜", "诸葛亮", "瑞文" };
			System.out.println(Arrays.toString(arr));
			System.out.println("输入S:开始第" + n + "轮的点名" + "\n" + "输入E:结束点名");
			String hand1 = sc.next();
			String[] newArr = new String[arr.length];
			if ("S".equals(hand1)) {
				int s = 10;
				while (arr.length != 0) {
					int num = (int) (Math.random() * s);
					System.out.println(getName(arr, num));
					newArr[n] = getName(arr, num);
					System.out.println("未被金蛋砸中的名单:"
							+ Arrays.toString(deleteName(arr, num)));
					System.out.println("输入C:继续本轮点名" + "\n"
							+ "输入E:结束本轮点名,进行下一轮点名");
					String hand2 = sc.next();
					if ("C".equals(hand2)) {
						arr = deleteName(arr, num);
					} else if ("E".equals(hand2)) {
						break;
					}
					s--;
				}
				System.out.println("------------------【第" + n
						+ "轮点名结束】----------------------");
				n++; // 每轮结束n++
			} else if ("E".equals(hand1)) {
				break;
			}
		}
	}

	// 定义一个输入索引返回对应元素的方法
	public static String getName(String[] arr, int num) {
		return arr[num];
	}

	// 定义一个删除数组中已经输出过的元素的方法
	public static String[] deleteName(String[] arr, int num) {
		String[] newArr = new String[arr.length];
		int m = 0;
		for (int i = 0; i < newArr.length; i++) {
			if (arr[num] != arr[i]) {
				newArr[m] =arr[i];
				m++;
			}
		}
		newArr = Arrays.copyOf(newArr, m);
		return newArr;
	}

}

你可能感兴趣的:(杂记,java,排序算法,算法)