一个简单的二十一点游戏

时间:2018-06-19 20:07:38

标签: java printing

我正在使用面向对象编程来制作二十一点游戏。

到目前为止我写的是这个

geom_bar()

我得到的输出是geom_histogram(),然后是import java.util.Random; public class Blackjack { public static String[] Card = new String[12]; public static String[] Suits = {"D", "H", "S", "C"}; public void setCard(String[] c) { Card = c; Card[0] = "A"; Card[1] = "2"; Card[2] = "3"; Card[3] = "4"; Card[4] = "5"; Card[5] = "6"; Card[6] = "7"; Card[7] = "8"; Card[8] = "9"; Card[9] = "10"; Card[10] = "J"; Card[11] = "Q"; Card[12] = "K"; } public static void main(String[] args) { System.out.println("Welcome to Blackjack."); Random rand = new Random(); int card_Sign1 = (int) (Math.random() * 13); int card_Sign2 = (int) (Math.random() * 13); int Suit1 = (int) (Math.random() * 4); int Suit2 = (int) (Math.random() * 4); System.out.print(Card[card_Sign1]); } } 。我想知道我能否在解决该问题上得到一些帮助。

2 个答案:

答案 0 :(得分:1)

正如csmckelvey在评论中所说,您需要在main方法内部调用setCard方法。否则,卡阵列将永远无法获得其初始值。

您还有另一个问题-您声明的数组大小为12:

public static String[] Card = new String [12];

但是您尝试在setCard中为其分配13个值。您需要将其更改为大小13。

此外,setCard()不需要参数,只需要设置Card的值就可以了。

除此之外,看起来还不错,请坚持下去!

答案 1 :(得分:1)

您忘记了对setCard()的调用,并且不需要将参数传递给setCard()。试试这个:

public static void main(String [] args) {

System.out.println("Welcome to Blackjack.");

Random rand = new Random();

int card_Sign1 = (int)(Math.random()*13);
int card_Sign2 = (int)(Math.random()*13);

int Suit1 = (int)(Math.random()*4);
int Suit2 = (int)(Math.random()*4);

setCard();

System.out.print(Card[card_Sign1]); 

} 

public void setCard() {
    Card[0] = "A";
    Card[1] = "2";
    Card[2] = "3";
    Card[3] = "4";
    Card[4] = "5";
    Card[5] = "6";
    Card[6] = "7";
    Card[7] = "8";
    Card[8] = "9";
    Card[9] = "10";
    Card[10] = "J";
    Card[11] = "Q";
    Card[12] = "K";
}