【发布时间】:2019-12-13 10:54:36
【问题描述】:
我正在尝试制作一个非常基本的 Black Jack 游戏,我将它分为三个简单的类,但我对 Java 非常陌生,而且我是一个糟糕的编码器,但我需要学习这个才能工作。我在理解我应该如何调用方法和类时遇到了一些重大困难。我弄清楚了游戏的基本结构,例如如何创建卡片以及如何进入游戏和退出游戏。我只是无法弄清楚游戏本身。这是我到目前为止所创造的。请任何建议和指导,这样我才能理解这将是伟大的,我很绝望。
BlackJack.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);
}
}
}
Deck.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);
}
}
【问题讨论】:
-
此类游戏通常有一个
while循环,其条件表明游戏仍在进行中。放开逻辑,找出一个简单的 while 循环,它将继续。对于初学者来说,它可能是while user does not hit q, get input..旁注,你不需要多个扫描仪,永远不要忘记scanner.close -
你可以用
System.out.println方法代替System.out.print,这样你就可以不用在每一行后面加上\n
标签: java string arraylist blackjack