理解Java中的方法/类调用

理解Java中的方法/类调用,java,string,arraylist,blackjack,Java,String,Arraylist,Blackjack,我正在尝试制作一个非常基本的Black Jack游戏,我把它分成了三个简单的类,但我对Java非常陌生,而且我是一个糟糕的程序员,但我需要在工作中学习这个。我很难理解如何调用方法和类。我弄明白了游戏的基本结构,比如如何制作卡片,如何进入游戏和退出游戏。我就是搞不懂游戏本身。这就是我迄今为止所创造的。请提供任何建议和指导,以便我能理解这将是伟大的我绝望 BlackJack.java import java.util.*; 公营黑帮4{ 公共静态void main(字符串[]args){ //在这里

我正在尝试制作一个非常基本的Black Jack游戏,我把它分成了三个简单的类,但我对Java非常陌生,而且我是一个糟糕的程序员,但我需要在工作中学习这个。我很难理解如何调用方法和类。我弄明白了游戏的基本结构,比如如何制作卡片,如何进入游戏和退出游戏。我就是搞不懂游戏本身。这就是我迄今为止所创造的。请提供任何建议和指导,以便我能理解这将是伟大的我绝望

BlackJack.java

import java.util.*;
公营黑帮4{
公共静态void main(字符串[]args){
//在这里编写代码
扫描仪键盘=新扫描仪(System.in);
扫描仪扫描=新扫描仪(System.in);
字符串游戏=”;
System.out.print(“想玩21点吗?\n”);
系统输出打印(“键入是或否”);
playGame=键盘.nextLine();
如果(playGame.equals(“是”))
{
/*
这是我需要帮助找出的领域。
*/
}
else if(playGame.equals(“no”)//玩家决定不玩游戏
{
System.out.print(“感谢您今天与我们一起玩。\n”);
System.out.print(“要退出游戏,请按enter。”);
scan.nextLine();
系统出口(0);
}
其他的
{
System.out.print(“对不起,您做错了,请再试一次。\n”);
System.out.print(“要退出游戏,请按enter。”);
scan.nextLine();
系统出口(0);
}
}
}
Deck.java

import java.util.*;
公务舱甲板{
ArrayList cards=新的ArrayList();
字符串[]套装={“梅花”、“钻石”、“红心”、“黑桃”};
字符串[]排名={null,“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”};
公共甲板(){
int d=Suits.length*Ranks.length;
字符串[]组=新字符串[d];
for(int i=0;i
Cards.java

public class Cards {
    private String suit;
    private String rank;

    public Cards(){}

    public Cards(String suit, String rank){
        this.suit = suit;
        this.rank = rank;
    }

    public  String getSuit(){
        return suit;
    }
    /* public void setSuit(String suit){
        this.suit = suit;
    }*/
    public String getRank(){
        return rank;
    }
    /*
    public void setRank(String value){
        this.rank = rank;
    }*/
    @Override
    public String toString() {
        return String.format("%s of %s", rank, suit);
    }
}

现在我对Java还比较陌生,所以我假设会有更多知识的人来帮助您,但这就是我目前所拥有的:

1) 我建议您删除
public Cards(){}构造函数
,因为您可能会意外地创建一张无效的卡(没有设置任何属性)

2) 如评论所述,我将执行while循环,检查是否按下某个键,然后执行相关操作

while (gameIsRunning) {
   if (key1 is pressed) {
   ...
   }

   ...

   if (key10 is pressed) {
   ...
   System.out.println("You won! Game over.");
   gameIsRunning = false;
   }
}
我会在一个类或方法的某个地方定义一轮游戏的过程,或者为它编写所有的helper方法,然后将它们放入这个循环结构中。 或者,您可以用文本提示用户,并等待下一个操作的文本输入


希望我能帮忙

现在我对Java还比较陌生,所以我认为会有更多知识的人来帮助你,但这就是我现在所拥有的:

1) 我建议您删除
public Cards(){}构造函数
,因为您可能会意外地创建一张无效的卡(没有设置任何属性)

2) 如评论所述,我将执行while循环,检查是否按下某个键,然后执行相关操作

while (gameIsRunning) {
   if (key1 is pressed) {
   ...
   }

   ...

   if (key10 is pressed) {
   ...
   System.out.println("You won! Game over.");
   gameIsRunning = false;
   }
}
我会在一个类或方法的某个地方定义一轮游戏的过程,或者为它编写所有的helper方法,然后将它们放入这个循环结构中。 或者,您可以用文本提示用户,并等待下一个操作的文本输入


希望我能帮忙

这类任务不是很简单,这里有很多事情要做,也许你必须从一些更基本的东西开始。但是,如果您确定要通过,这里有一些建议和代码: 您必须将
score
字段添加到Card类中(对于类而言,单个名称优于复数,对于变量而言,首字母小写,这是一些代码约定)。不容易,因为Ace可以有多值。 将LinkedList而不是ArrayList用作Deck.cards。当庄家每次投票一张牌时,这和真实游戏更相似

放入
这个.cards.add(卡片)甲板[suites.length*i+j]=等级[i]+“+suites[j];”的整数您根本不需要这个字符串数组。最重要的部分是你可以把权杖放在你所指的地方,那里最需要帮助:


    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();
    }


私人静态void游戏(扫描仪扫描){
甲板=新甲板();
弦乐游戏;
整数分数=0;
随机=新随机();
布尔PlayerRough=false;
布尔值dealeRough=false;
整数dealerScore=0;
洗牌();

虽然((yourScore这类任务不是很简单,但这里有很多事情要做,也许你必须从一些更基本的东西开始。但是如果你确定你想完成,这里有一些建议和代码: 您必须将
score
字段添加到Card类中(对于类而言,单个名称优于复数,对于变量而言,首字母小写更好,这是一些代码约定)。这并不容易,因为Ace可能具有多值。 使用LinkedList而不是ArrayList作为Deck.cards。当庄家每次轮询一张牌时,它更类似于真实游戏

放置
this.cards.add(cards);
interead of'deck[Suits.length*i+j]=秩[i]+“of”+Suits[j];“你根本不需要这个字符串数组。最重要的部分是你可以把权杖放在你指向的地方,那里最需要帮助:


    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();
    }


私人静态void游戏(扫描仪扫描){
甲板=新甲板();