在我开始之前,是的,这是一项家庭作业,所以显然我不是在要求任何答案,但如果有人能在正确的方向上帮助我或给我一些建议,我将不胜感激。
好的,所以对于一项任务,我正在用 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");
}
}
}