Java來實(shí)現(xiàn)游戲
目的
- 能夠知道如何建立package和class以及它們與文件的關(guān)系
- 能夠在Java當(dāng)中使用main函數(shù)
- 懂得如向Java中輸入數(shù)據(jù)以及如何創(chuàng)建數(shù)組
技術(shù)
建立一個類,注意一個package里面只能有一個public類,運(yùn)用main函數(shù)時,可以直接打上去就行了代碼引用 public static void main(String[] args){}
輸入數(shù)據(jù)用scanner函數(shù)就可以了,并在引用的時候創(chuàng)建數(shù)組,創(chuàng)建數(shù)組如果要給數(shù)組賦值時要知道數(shù)組里面又多少個存放數(shù)據(jù)的,代碼引用
int num = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入?yún)⑴c人數(shù)");
num = scanner.nextInt();
int[] temp = new int[num];
具體代碼的實(shí)現(xiàn)
class Killman {
public static void main(String[] args) {
int num = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入?yún)⑴c人數(shù)");
num = scanner.nextInt();
int[] temp = new int[num];
for (int i = 0; i< num; i++) {
temp[i] = i + 1;
}
System.out.print("請輸入死亡編號:");
int KilledNumber = scanner.nextInt();
int Killed = -1;
int count = 0;
int totalKilled = 0;
for (int i = 0; i < num; i++) {
if (temp[i] != Killed) {
count++;
if (count == KilledNumber) {
System.out.println(temp[i]);
count = 0;
temp[i] = Killed;
totalKilled++;
if (totalKilled == num - 1) {
break;
}
}
}
if (i == num -1){
i = -1;
}
}
for (int i = 0; i < num; i++) {
System.out.print(temp[i] + "");
}
}
}
就是把已經(jīng)死亡的標(biāo)記為-1,并且可以自己選擇參與人數(shù)與死亡編號,只要從一開始數(shù)到那個數(shù)字就直接淘汰。