JavaScript新手求助:如何实现从牌组随机抽卡并统计各花色数量?
Hey there! Let's get your card-drawing program working smoothly. You’ve already spotted the two key issues, so let’s walk through fixing each one step by step.
1. Fixing the takeCard() Function
Your current logic here adds either 1 or 3 cards per loop iteration, which means you’ll never end up with exactly NUM cards. We need to pick one random suit per loop to guarantee we draw the exact number the user requested.
Here’s the revised function:
function takeCard(){ var pick = []; var suits = [DIAMONDS, HEARTS, SPADES, CLUBS]; // Store all suits in an array for easy random selection for(var i = 0; i < NUM; i++){ // Pick a random index (0-3) to select one suit var randomIndex = Randomizer.nextInt(0, suits.length - 1); pick.push(suits[randomIndex]); } return pick; }
Now every loop iteration adds exactly one random suit to the array, so after NUM loops, you’ll have exactly NUM cards.
2. Fixing the countCards() Function
The original function checks for a non-existent value ("Heads") and only tracks two suits, leaving Spades and Clubs stuck at 0. We need to properly check each of the four suits and increment their counters.
A clean approach uses a switch statement for clarity:
// Counts the number of Cards in each suit function countCards(take){ var diamondCount = 0; var heartCount = 0; var spadeCount = 0; var clubCount = 0; for(var i = 0; i < take.length; i++){ switch(take[i]){ case DIAMONDS: diamondCount++; break; case HEARTS: heartCount++; break; case SPADES: spadeCount++; break; case CLUBS: clubCount++; break; } } println("Number of Diamonds: " + diamondCount); println("Number of Hearts: " + heartCount); println("Number of Spades: " + spadeCount); println("Number of Clubs: " + clubCount); }
This checks each card’s suit and increments the corresponding counter. If you prefer, you could also use an if/else if chain instead of the switch statement.
Full Corrected Code
Putting it all together, here’s the working version of your program:
var NUM = readInt("How many cards will you draw from the deck?"); var DIAMONDS = "Diamonds"; var HEARTS = "Hearts"; var SPADES = "Spades"; var CLUBS = "Clubs"; function start(){ var take = takeCard(); printArray(take); countCards(take); } // This function picks a random card from the deck function takeCard(){ var pick = []; var suits = [DIAMONDS, HEARTS, SPADES, CLUBS]; for(var i = 0; i < NUM; i++){ var randomIndex = Randomizer.nextInt(0, suits.length - 1); pick.push(suits[randomIndex]); } return pick; } // Displays the drawn cards function printArray(arr){ for(var i = 0; i < arr.length; i++){ println("Flip Number " + (i+1) + ": " + arr[i]); } } // Counts the number of cards in each suit function countCards(take){ var diamondCount = 0; var heartCount = 0; var spadeCount = 0; var clubCount = 0; for(var i = 0; i < take.length; i++){ switch(take[i]){ case DIAMONDS: diamondCount++; break; case HEARTS: heartCount++; break; case SPADES: spadeCount++; break; case CLUBS: clubCount++; break; } } println("Number of Diamonds: " + diamondCount); println("Number of Hearts: " + heartCount); println("Number of Spades: " + spadeCount); println("Number of Clubs: " + clubCount); }
Now when you run the program, it will draw exactly the number of cards the user asks for, and correctly count how many of each suit were drawn.
内容的提问来源于stack exchange,提问作者Jimmy O'Malley

