jeudi 7 avril 2016

Using Randoms in Separate Methods

Could I put a random into a method separate from the main class?

    while (blackjack == true) {

        String[] CardBoard = new String[52];

        boolean checker = true;

        int total = 0;
        int cheatcheck = 0;

        // Card Checker; This will use the process of checking for if the card is taken
        // Using this will make sure no cards are duplicated
        for (int x = 0; x < 52; x++) {
            CardBoard[x] = "[ ]";

        }

        Scanner keyboard = new Scanner(System.in);

        Random blackJack = new Random();

        // This will ALWAYS come out with a new number, all the time
        public static int randomGenerator  {
                int Card1 = blackJack.nextInt(52);
                int Card2 = blackJack.nextInt(52);

                if (Card1 == Card2) {
                    if (Card2 == 52) {
                        Card2--;
                    } else {
                        Card2++;
                    }
                }
                return Card1;
                return Card2;
        }

        // This will be removed when done; it is only a developer note.
        System.out.println(Card1);
        System.out.println(Card2);



        while (gameplay == true) {  
            while (checker == true) {

                CardBoard[randomGenerator(Card1)] = "[#]";
                CardBoard[randomGenerator(Card2)] = "[#]";

                if (card1 > 5) {
                    total = total + 1;
                } else if (card1 > 9 && card1 < 4) {
                    total = total + 2;
                } else if (card1 > 13 && card1 < 8) {
                    total = total + 3;
                } else if (card1 > 17 && card1 < 12) {
                    total = total + 4;
                } else if (card1 > 21 && card1 < 16) {
                    total = total + 5;
                } else if (card1 > 25 && card1 < 20) {
                    total = total + 6;
                } else if (card1 > 29 && card1 < 24) {
                    total = total + 7;
                }

            }

I'm trying to pick random numbers but also trying to keep it outside the code so that it just doesn't become cluttered.

Would there be a way to work the random in the separate method or would I need to change the current code?




Aucun commentaire:

Enregistrer un commentaire