用Java來寫簡單游戲

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ù)字就直接淘汰。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容