第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

了解 Java 中的方法/類調(diào)用

了解 Java 中的方法/類調(diào)用

猛跑小豬 2023-05-17 17:54:12
我正在嘗試制作一個非?;镜?Black Jack 游戲,我將它分為三個簡單的類,但我對 Java 非常陌生,而且我是一個糟糕的編碼員,但我需要學(xué)習(xí)它才能工作。我很難理解我應(yīng)該如何調(diào)用方法和類。我想出了游戲的基本結(jié)構(gòu),比如如何創(chuàng)建卡牌以及如何進(jìn)入游戲和退出游戲。我只是無法弄清楚游戲本身。這是我到目前為止所創(chuàng)建的。請?zhí)峁┤魏谓ㄗh和指導(dǎo),這樣我才能理解這太好了,我很絕望。BlackJack.javaimport java.util.*;public class BlackJack4 {    public static void main(String[] args) {    // write your code here        Scanner keyboard = new Scanner(System.in);        Scanner scan = new Scanner(System.in);        String playGame = "";        System.out.print("Wanna play some BlackJack? \n");        System.out.print("Type yes or no \n");        playGame = keyboard.nextLine();        if (playGame.equals ("yes"))        {            /*            This is the area I need help in figuring out.              */        }        else if (playGame.equals("no")) //Player decided to no play the game        {            System.out.print("Thank you for playing with us today.\n");            System.out.print("To exit the game please press enter.");            scan.nextLine();            System.exit(0);        }        else        {            System.out.print("Sorry you did something wrong. Please try again.\n");            System.out.print("To exit the game please press enter.");            scan.nextLine();            System.exit(0);        }    }}Deck.javaimport java.util.*;public class Deck {    ArrayList<Cards> cards = new ArrayList<Cards>();    String[] Suits = { "Clubs", "Diamonds", "Hearts", "Spades"};    String[] Ranks = {null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};    public Deck() {        int d = Suits.length * Ranks.length;        String[] deck = new String[d];        for (int i = 0; i < Ranks.length; i++) {            for (int j = 0; j < Suits.length; j++) {                deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];            }        }    }    public void shuffle(){//shuffle the deck when its created        Collections.shuffle(this.cards);    }}
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊

這種任務(wù)不是很容易的水平,這里有很多事情要做,也許你必須從一些更基本的東西開始。但是,如果您確定要通過,這里有一些建議和代碼:您必須score向 Card 類添加字段(類的單個名稱優(yōu)于復(fù)數(shù),變量的首字母小寫,這是一些代碼約定) . 不容易,因為 A 可以具有多值。使用 LinkedList 而不是 ArrayList 作為 Deck.cards。發(fā)牌者每次輪詢一張牌,這與真實游戲更相似。


把this.cards.add(cards);'deck [Suits.length * i + j] = Ranks [i] + " of " + Suits [j];' 你根本不需要這個字符串?dāng)?shù)組。最重要的部分是您可以將骨架放在您指向的地方,最需要幫助的地方:



    private static void playGame(Scanner scan) {

        Deck deck = new Deck();

        String playGame;

        Integer yourScore = 0;

        Random random = new Random();

        Boolean playerEnough = false;

        Boolean dealerEnough = false;

        Integer dealerScore = 0;

        deck.shuffle();

        while ((yourScore <= 21 && dealerScore <= 21) && (!dealerEnough || !playerEnough)) {

            if (yourScore == 0) {

                yourScore += getCardScore(deck, "Player");

                yourScore += getCardScore(deck, "Player");

            }

            if (dealerScore == 0) {

                dealerScore += getCardScore(deck, "Dealer");

                dealerScore += getCardScore(deck, "Dealer");

            }

            if (!playerEnough) {

                System.out.println("Want a card?");

                playGame = scan.nextLine();

                if (playGame.equals("yes")) {

                    yourScore += getCardScore(deck, "Player");

                } else {

                    System.out.println("PlayerDone");

                    playerEnough = true;

                }

            }

            if (!dealerEnough) {

                if (random.nextBoolean()) {

                    dealerScore += getCardScore(deck, "Dealer");

                } else {

                    System.out.println("DealerDone");

                    dealerEnough = true;

                }

            }

            System.out.println(yourScore + " [p] : [d] " + dealerScore);    

        }

        // decide who is a winner

    }



    private static Integer getCardScore(Deck deck, String who) {

        System.out.print(who + " given: ");

        Cards cards = deck.cards.pollFirst();

        System.out.println(cards);

        return cards.getScore();

    }


這可以幫助你更進(jìn)一步,或者沒有,那么我建議你解決一些較小的練習(xí)。


查看完整回答
反對 回復(fù) 2023-05-17
?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗 獲得超6個贊

一種有點過時但仍然非常有效的軟件設(shè)計方法稱為瀑布過程:從最頂層開始,逐步深入到細(xì)節(jié),而不是相反。比如先從游戲的整體流程說起:


public static class Game {

    public static void main(String args[]) {

        while (/* player wishes to continue */) {

            /* play a hand */

        }

        System.out.println('Thank you for playing.');

    }

}

現(xiàn)在,考慮需要為上面的評論做些什么。你希望如何與玩家溝通以獲得他的意圖?鍵盤?圖形用戶界面?這將決定您如何充實該部分。然后決定“玩一手牌”部分的總體策略,你會得到一個新的水平:


public class Game {

    . . .

     public void playOneHand(Player p) {

         /* create new shuffled deck */

         /* deal initial cards */

         while (/* player has choices */) {

             /* get player choices */

         }

         /* play dealer hand */

         /* display result */

     }

}

public class Player {

    /* Maybe actions should be numbers from a menu? An enum class? */

    public String getPlayerAction(String prompt) {

        Scanner sc = new Scanner(System.in);

        System.out.println(prompt);

        return sc.nextLine();

    }

}

那是第 2 級?,F(xiàn)在開始填寫每條評論,這將帶您進(jìn)入第 3 級,依此類推。最終你會深入了解卡片和數(shù)字的細(xì)節(jié),但直到你準(zhǔn)備好將它們放在某個地方。


查看完整回答
反對 回復(fù) 2023-05-17
?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗 獲得超10個贊

現(xiàn)在我對 Java 比較陌生,所以我假設(shè)會有更多知識的人出現(xiàn)并能夠為您提供更多幫助,但這就是我現(xiàn)在所擁有的:


1)我建議你刪除,public Cards() {} constructor因為你可能會不小心創(chuàng)建一個無效的卡(沒有設(shè)置任何屬性)。


2)如評論所述,我會做一個 while 循環(huán)來檢查是否按下了一個鍵,然后執(zhí)行相關(guān)操作。


while (gameIsRunning) {

   if (key1 is pressed) {

   ...

   }


   ...


   if (key10 is pressed) {

   ...

   System.out.println("You won! Game over.");

   gameIsRunning = false;

   }

}

我會在一個類或方法的某個地方定義一輪游戲的過程,或者為它編寫所有的輔助方法,然后將它們放入這個循環(huán)結(jié)構(gòu)中?;蛘?,您可以用文本提示用戶并等待文本輸入以進(jìn)行下一步操作。


希望我能幫上忙!


查看完整回答
反對 回復(fù) 2023-05-17
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號