1

在我开始之前,是的,这是一项家庭作业,所以显然我不是在要求任何答案,但如果有人能在正确的方向上帮助我或给我一些建议,我将不胜感激。

好的,所以对于一项任务,我正在用 java 制作一个黑杰克游戏。目前我有一个 Card 类、一个 Deck 类和我的 Main (CardGameTester)。现在我只和一个玩家(用户)一起玩游戏,所以它非常基础。我的主班很乱,我可以参加第三节课,但我决定改用这种方式。

我遇到了几个奇怪的错误,我相信这些错误是我弄乱了我的数组列表或与对象有关的东西。如果有人可以帮助我,我将不胜感激,因为我现在很困。

我尝试编译时遇到的错误:

6 errors and 1 warning found:
--------------
*** Errors ***
--------------
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java  [line: 17]
Error: The method add(Card) in the type java.util.ArrayList<Card> is not applicable for the arguments (Deck)
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java  [line: 28]
Error: The method add(Card) in the type java.util.ArrayList<Card> is not applicable for the arguments (Deck)
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java  [line: 35]
Error: Type mismatch: cannot convert from Card to int
File: E:\Documents\Java Files\Assignment 3\Deck.java  [line: 15]
Error: The constructor Card() is undefined
File: E:\Documents\Java Files\Assignment 3\Deck.java  [line: 19]
Error: The method shuffle() is undefined for the type java.util.ArrayList<Card>
File: E:\Documents\Java Files\Assignment 3\Deck.java  [line: 30]
Error: c cannot be resolved
-------------
** Warning **
-------------
File: E:\Documents\Java Files\Assignment 3\Card.java  [line: 5]
Warning: The field Card.suits is never read locally

这是 Card.java

public class Card
{
  private int rankValue, suitValue;
  private String ranks[] = {"Hearts", "Clubs", "Spades", "Diamonds"};
  private String suits[] = {"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

  public Card(int rank, int suit)
  {
    rankValue = rank;
    suitValue = suit;
  }

  public String convertToString(int rank){
    return ranks[rank];
  }

  public void setRank(int rank){
    rankValue = rank;
  }

  public void setSuit(int suit){
    suitValue = suit;
  }

  public int getRank(){
    return rankValue;
  }

  public int getSuit(){
    return suitValue;
  }

  public String toString(){
    return "Rank: " + rankValue + "Suit: " + suitValue;
  }
}

这是 Deck.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

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

  public Deck()
  {
    for(int a = 1; a <= 4; a++)
    {
      for(int b = 1; b <= 13; b++)
      {
        Card c = new Card();
        cards.add(c);
      }
    }
    cards.shuffle();
  }

  public void shuffle(){
    Collections.shuffle(cards);
  }

  public String deal(){
    int index = 0;
    cards.get(index);
    cards.remove(index);
    return "You have been dealt a: " + c.toString();
    index++;
  }
}

这是我的主要 CardGametester.java:

public class CardGameTester
{
  public static void main(String[] args)
  {
    ArrayList<Card> hand = new ArrayList<Card>();
    Deck d = new Deck();

    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to the game of Black Jack!\nWould you like to Start?");
    String input = scan.nextLine();
    if(input == "yes")
    {
      d.deal();
      hand.add(d);
    }

    String input2;

    while (input2 == "yes")
    {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Would you like to hit or stay?");
      input2 = keyboard.nextLine();
      d.deal();
      hand.add(d);
    }

    int total;

    for(int index = 0; index <= hand.size(); index++)
    {
      total = hand.get(index);
    }

    System.out.println("Your total value for you cards are: " + total);

    if(total <= 21){
      System.out.println("Congrats, you have won for not going over 21");
    }
    else
      {
        System.out.println("Sorry, you lose for going over 21");
    }
  }
}
4

1 回答 1

0

您有一个ArrayListofCard但正在尝试添加不同类型的Deck:

ArrayList<Card> hand = new ArrayList<Card>();

hand.add(d); // d = Deck

最好在 中返回卡的索引Deck.deal以跟踪您的hand.

另外,您正在调用:

Card c = new Card();

但构造函数有 2 个参数:

public Card(int rank, int suit)

deal方法有2个错误:

...
return "You have been dealt a: " + c.toString();
index++;
...

首先在Card c这里定义。你可以使用:

Card c = cards.remove(index);

其次,语句后面不能有return语句,所以index++不能出现在这里。

下一个:

错误:类型不匹配:无法从 Card 转换为 int

    total = hand.get(index);
      ^            ^
   int type     Card type

您正在调用一个不存在的方法ArrayList

cards.shuffle();

你已经在一个方法中有这个,所以你可以调用

shuffle();

虽然不是编译错误,但使用==会导致您的输入字符串无法正确评估,因此请更改

if (input == "yes")

if ("yes".equals(input))

考虑使用枚举来管理卡片等级和花色。

于 2012-10-12T23:41:29.403 回答