在我开始之前,是的,这是一项家庭作业,所以很明显我不是要求任何答案,但如果有人能帮助我朝正确的方向或给我一些建议,我将不胜感激。
好的,对于作业我在java中制作黑杰克游戏。目前我有一个Card类,一个Deck Class和我的Main(CardGameTester)。现在我只与一个玩家(用户)进行游戏,所以它非常基本。我的主要是非常混乱,我可以做三等课,但我决定这样做。
我遇到了几个奇怪的错误,我认为这些错误让我搞乱了我的arraylists或与对象有关。如果有人能帮助我,我会非常感激,因为我现在很困惑。
我尝试编译时遇到的错误:
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");
}
}
}
答案 0 :(得分:0)
您有ArrayList
Card
但是正在尝试添加其他类型的Deck
:
ArrayList<Card> hand = new ArrayList<Card>();
hand.add(d); // d = Deck
最好在Deck.deal
中返回卡片的索引,以便跟踪hand
中的卡片。
另外,您正在致电:
Card c = new Card();
但构造函数有两个参数:
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))
考虑使用enums来管理卡级别&amp;服。