gpt4 book ai didi

java - 纸牌 Java 程序

转载 作者:行者123 更新时间:2023-11-30 02:01:48 26 4
gpt4 key购买 nike

嘿伙计们,我正在学习我的第一个 Java 类(class),但在尝试编译该程序时遇到了错误代码。我附加了这两门类(class),希望他能帮助我找到错误。这是我收到的错误:

Error: constructor Card in class Card cannot be applied to given types; required: char,char found: no arguments reason: actual and formal argument lists differ in length

它显示错误发生在“Card temp= new Card();”行上在 public void shuffle 方法下。任何帮助将不胜感激。

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;
}
}

最佳答案

您的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 block 中,因为它仅在该范围内使用。

关于java - 纸牌 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52615821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com