【发布时间】:2014-08-27 08:39:42
【问题描述】:
我还是新手,我在这个项目中遇到了困难。我需要洗牌并得到前四张牌并计算四张牌的总和。到目前为止,我已经完成了这项任务。现在我的问题是我需要重复这个方法,直到我能得到 24 的总数。我很难知道如何循环这个方法。这是我到目前为止所做的代码:
public class cards
{
public static void main(String[] args)
{
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int rankedVal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int tot = 0;
if (tot != 24){
//Initialize cards
tot = 0;
for (int i = 0; i < deck.length; i++)
deck[i] = i;
//Shuffle cards
for (int i = 0; i < deck.length; i++)
{
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//Display first four
for (int i = 0; i < 4; i++)
{
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
}
//find the total
for (int i = 0; i<4;i++){
tot = tot + rankedVal[deck[i] % 13];
}
System.out.println("Total Hand = " + tot);
}
}
}
【问题讨论】:
-
你知道你用的是什么编程语言吗?
-
好问题@Jongware..copy/paste?
-
@BeEasyImNoob..到目前为止您已经完成了,但您不知道如何在提取和求和中循环?
-
我猜是Java。一副纸牌和它的操作经常困扰新程序员,但我更担心表示和操作而不是计算一个总和......。