首页 > 解决方案 > 理解 Java 中的方法/类调用

问题描述

我正在尝试制作一个非常基本的 Black Jack 游戏,我将它分为三个简单的类,但我对 Java 非常陌生,而且我是一个糟糕的编码器,但我需要学习这个才能工作。我在理解我应该如何调用方法和类时遇到了一些重大困难。我弄清楚了游戏的基本结构,例如如何创建卡片以及如何进入游戏和退出游戏。我只是无法弄清楚游戏本身。这是我到目前为止所创造的。请任何建议和指导,这样我才能理解这将是伟大的,我很绝望。

黑杰克.java

import java.util.*;

public class BlackJack4 {

    public static void main(String[] args) {
    // write your code here

        Scanner keyboard = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        String playGame = "";

        System.out.print("Wanna play some BlackJack? \n");
        System.out.print("Type yes or no \n");

        playGame = keyboard.nextLine();

        if (playGame.equals ("yes"))
        {
            /*
            This is the area I need help in figuring out. 
             */
        }
        else if (playGame.equals("no")) //Player decided to no play the game
        {
            System.out.print("Thank you for playing with us today.\n");
            System.out.print("To exit the game please press enter.");
            scan.nextLine();
            System.exit(0);
        }
        else
        {
            System.out.print("Sorry you did something wrong. Please try again.\n");
            System.out.print("To exit the game please press enter.");
            scan.nextLine();
            System.exit(0);
        }
    }
}

甲板.java

import java.util.*;

public class Deck {
    ArrayList<Cards> cards = new ArrayList<Cards>();

    String[] Suits = { "Clubs", "Diamonds", "Hearts", "Spades"};
    String[] Ranks = {null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    public Deck() {
        int d = Suits.length * Ranks.length;

        String[] deck = new String[d];
        for (int i = 0; i < Ranks.length; i++) {
            for (int j = 0; j < Suits.length; j++) {
                deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];
            }
        }
    }

    public void shuffle(){//shuffle the deck when its created
        Collections.shuffle(this.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);
    }
}

标签: javastringarraylistblackjack

解决方案


现在我对 Java 比较陌生,所以我假设会有更多知识的人出现并能够为您提供更多帮助,但这是我现在所拥有的:

1)我建议您删除,public Cards() {} constructor因为您可能会意外创建无效卡(未设置任何属性)。

2)正如评论所述,我会做一个while循环来检查按键,然后执行相关操作。

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

   ...

   if (key10 is pressed) {
   ...
   System.out.println("You won! Game over.");
   gameIsRunning = false;
   }
}

我会在一个类或方法的某个地方定义一轮游戏的过程,或者为它编写所有的辅助方法,然后将它们放入这个循环结构中。或者,您可以使用文本提示用户并等待文本输入以进行下一步操作。

希望我能帮上忙!


推荐阅读