伙计们,所以我要上我的第一个Java类,并在尝试编译该程序时遇到错误代码。我把这两节课都附上了,希望他能帮助我找到错误。这是我收到的错误:
错误:Card类中的构造函数Card无法应用于给定类型; 必填:char,char 找到:没有参数 原因:实际和正式论据列表的长度不同
它显示错误在“ Card temp = new Card();”行上根据公共无效洗牌法。任何帮助将不胜感激。
import java.util.Random;
import java.util.Scanner;
public class Deck {
private Card [] data;
public Deck()
{
String suits = "HDSC";
String ranks = "A23456789TJQK";
data = new Card[52];
int count = 0;
Card C1;
for (int s = 0; s < suits.length(); s++){
for (int r = 0; r < ranks.length(); r ++)
{
C1 = new Card(ranks.charAt(r), suits.charAt(s));
data[count++] = C1;
}
}
}
//This function display's the whole deck of cards
// Our output should be as below
// AH 2H 3H ... KH
// AS 2H 3S ... KS
// AD 2D 3D ... KD
// AC 2C 3C ... KC
public void display()
{
int index=1;
for (int i=0; i<52; i++)
{
System.out.print(data[i].rank +"" + data[i].suit + " ");
if (index%13 == 0 && i!=0 )
System.out.println();
index++;
}
}
//This function randomly shuffles the deck of cards
public void shuffle()
{
int index;
Random random = new Random();
for (int i = 0; i<52; i++)
{
index = i + random.nextInt(52 - i);
Card temp= new Card();
if (index != i)
{
temp = data[i];
data[i] = data[index];
data[index] = temp;
}
}
System.out.println("Card Shuffled");
}
public void deal()
{
int sum = 0;
int countPrime = 0;
boolean isPrime = false;
for(int i=0; i<52; i++)
{
sum += data[i].getValue(data[i]);
isPrime = checkPrime(sum);
if (isPrime == true)
{
sum = 0;
countPrime++;
if (i==51)
{
System.out.println("Winner in " + countPrime + " Piles");
break;
}
}
if (i==51)
System.out.println("Loser");
}
}
boolean checkPrime(int num)
{
boolean isPrime = true;
for(int j = 2; j <= num/2; ++j)
{
// condition for nonprime number
if(num % j == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}
//Display menu
public static int menu() {
System.out.println("\nWelcome to Solitaire Prime!");
System.out.println("1) New Deck");
System.out.println("2) Display Deck");
System.out.println("3) Shuffle Deck");
System.out.println("4) Play Solitaire Prime");
System.out.println("5) Exit");
Scanner in=new Scanner(System.in);
int choice = in.nextInt();
return choice;
}
public static void main(String args[])
{
Deck newDeck = null;
int choice;
do{
//Accepts user input for menu
choice = menu();
if (choice == 1)
{
newDeck = new Deck();
System.out.println("New deck created");
}
else if (choice == 2 )
{
newDeck.display();
}
else if (choice == 3 )
{
newDeck.shuffle();
}
else if (choice == 4 )
{
newDeck.shuffle();
newDeck.deal();
}
else if (choice == 5)
{
System.out.println("Exiting game. Goodbye!");
return;
}
else
System.out.println("Wrong choice! Please try again.");
}while(choice!=5);
}
}
public class Card {
char suit;
char rank;
public Card(char r, char s)
{
rank = r;
suit = s;
}
public void menu()
{
}
public void display(Card C1)
{
char suit = getSuit(C1);
char rank = getRank(C1);
String suitName = "";
String rankName = "";
if (suit == 'S')
suitName = "Spade";
else if (suit == 'H')
suitName = "Hearts";
else if (suit == 'C')
suitName = "Clubs";
else if (suit == 'D')
suitName = "Diamonds";
if (rank == 'A')
rankName = "Ace";
else if (rank == '2')
rankName = "Two";
else if (rank == '3')
rankName = "Three";
else if (rank == '4')
rankName = "Four";
else if (rank == '5')
rankName = "Five";
else if (rank == '6')
rankName = "Six";
else if (rank == '7')
rankName = "Seven";
else if (rank == '8')
rankName = "Eight";
else if (rank == '9')
rankName = "Nine";
else if (rank == '1')
rankName = "Ten";
else if (rank == 'J')
rankName = "Jack";
else if (rank == 'Q')
rankName = "Queen";
else if (rank == 'K')
rankName = "King";
System.out.println(rankName + " of " + suitName);
}
//This method gives the value of a card
public int getValue(Card C1)
{
int value = 0;
if (C1.rank == 'A')
value = 1;
else if (C1.rank == '2')
value = 2;
else if (C1.rank == '3')
value = 3;
else if (C1.rank == '4')
value = 4;
else if (C1.rank == '5')
value = 5;
else if (C1.rank == '6')
value = 6;
else if (C1.rank == '7')
value = 7;
else if (C1.rank == '8')
value = 8;
else if (C1.rank == '9')
value = 9;
else if (C1.rank == '1')
value = 10;
else if (C1.rank == 'J')
value = 10;
else if (C1.rank == 'Q')
value = 10;
else if (C1.rank == 'K')
value = 10;
return value;
}
//This method gives the rank of a card
public char getRank(Card C1)
{
return C1.rank;
}
//This method gives the suit of a card
public char getSuit(Card C1)
{
return C1.suit;
}
}
答案 0 :(得分:2)
您的Card
类只有一个带有两个char
自变量的构造函数;没有定义零参数构造函数。
洗牌时,不需要像现在在这里那样创建Card
的新实例:Card temp= new Card();
而不是创建Card
的新实例并将其抛出只需将经过改组的Card
值分配给temp
变量,如下所示:
for (int i = 0; i<52; i++)
{
index = i + random.nextInt(52 - i);
if (index != i)
{
Card temp = data[i];
data[i] = data[index];
data[index] = temp;
}
}
请注意,我们将temp
移到了if块中,因为它仅在该范围内使用。