这是一项课堂作业,我不是要求任何人为我完成作业,而是我已经尽我所能,然后在这里发帖,希望能就我收到的 4 个错误和我的套牌没有得到一些帮助由于某种原因被洗牌。
我的作业方向:
对于这个任务,您将创建一个玩简单战争游戏的程序。在这个游戏中,每个玩家都会从一副完整的牌组中获得一张牌。谁拥有价值最高的牌,谁就获胜。如果所发的牌具有相同的价值,则为平局,双方均未获胜。赢得最多回合的玩家赢得游戏。玩家不需要输入(不是很有趣!)。您应该打印每个玩家收到的牌以及该轮的结果和游戏的最终结果。您可能希望使用用户输入来实现每轮之间的延迟。
我的卡类:
public class Card {
private int cardNum;
final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};
Card (int theCard) {
setCardNum (theCard);
}
public void setCardNum (int theCard) {
cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
}
public int getCardNum() {
return cardNum;
}
public String toString() {
return ranks[cardNum%13] + " of " + suits[cardNum/13];
}
public String getSuit() {
return suits[cardNum/13];
}
public String getRank() {
return ranks[cardNum%13];
}
public int getValue() {
return cardNum%13;
}
}
我的甲板课(我有一个洗牌错误):
public class Deck {
private Card[] deck = new Card[52];
private int topCard;
Deck() {
topCard = 0;
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(i);
}
public void shuffle() {
topCard = 0;
for (int i = 0; i < 1000; i++) {
int j = (int)(Math.random()*52);
int k = (int)(Math.random()*52);
Card tmpCard = deck[j];
deck[j] = deck[k];
deck[k] = tmpCard;
}
}
public Card dealCard() {
Card theCard;
if (topCard < deck.length) {
theCard = deck[topCard];
topCard++;
}
else
theCard = null;
return theCard;
}
}
我的战争游戏主程序:
import java.util.Scanner;
public class WarGame {
public static void main(String[] args) {
Card[][] hands = new Card[2][1];
Deck myDeck = new Deck();
for (int i = 0; i < 53; i++) {
System.out.printf("\n Round %s of The War \n", i);
for (int c = 0; c < 1; c++)
for (int player = 0; player < hands.length; player++)
hands[player][c] = myDeck.dealCard();
for (int player = 0; player < hands.length; player++) {
System.out.printf("Player %d: ", player);
printHand(hands[player]);
int player1;
int player2;
if (player1.getValue() > player2.getValue())
System.out.println("Player One Wins The War");
else if (player2.getValue() > player1.getValue())
System.out.println("Player Two Wins The War");
else
System.out.println("The War Is A Tie");
}
}
}
public static void printHand(Card[] hand) {
for (int card = 0; card < hand.length; card++)
System.out.printf("%s", hand[card].toString());
System.out.println();
}
}
我的错误如下:
----jGRASP exec: javac -g WarGame.java
WarGame.java:31: error: int cannot be dereferenced
if (player1.getValue() > player2.getValue())
^
WarGame.java:31: error: int cannot be dereferenced
if (player1.getValue() > player2.getValue())
^
WarGame.java:35: error: int cannot be dereferenced
else if (player2.getValue() > player1.getValue())
^
WarGame.java:35: error: int cannot be dereferenced
else if (player2.getValue() > player1.getValue())
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
非常感谢您提供的任何帮助。
player1 和 player2 指的是每个玩家每轮得到的牌