虽然在课堂上已经有冯佳老师带我们做过了。但是我想了。比起让我们自己写入多少%于多少的规则,不如让规则直接用随机数代替。这样,更能符合随机杀人的刺激感,也能测试当天的幸运数字是多少,何乐而不为呢。我是求精啊~~不多说。附上代码~
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class killPeoPleGame {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入有几个人参加了这场游戏?");
int people = sc.nextInt();
System.out.println("请输入你的号数?");
int player = sc.nextInt();
Random ra = new Random();
int rule = ra.nextInt(people) + 1;
int last = theLast(people, rule);
Thread.currentThread().sleep(1000);
System.out.println("请等待10秒,正在杀人");
Thread.currentThread().sleep(10000);
if (player == last) {
System.out.println("恭喜你" + last + "号,在这场" + people
+ "个人参加的死亡游戏里,最后你活了下来。^_^ ^_^");
} else {
System.out.println("对不起" + player + "号。你死亡了,活下来的人是" + last + "号!!");
}
}
public static int theLast(int people, int rule) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= people; i++) { // 让报名参加的游戏录入。
list.add(i);
}
int kill = 1; // 杀人器,从1号开始指
for (int i = 0; list.size() != 1; i++) { // 开始游戏,不剩最后一个人。游戏不结束!
if (i == list.size()) {
i = 0;
}
if (kill % rule == 0) {
list.remove(i--);
}
kill++;
}
return list.get(0);
}
}
|
|