用Java for 编写关于扑克牌的程序!急!
存储时可以用数字,最后显示时可以装换为字母
import java.util.Random;
public class Poker {
public static void main(String[] args) {
Random random = new Random();
int[] computer = new int[5];
int[] player = new int[5];
for (int i = 0; i 5; i++) {// 发牌
computer[i] = random.nextInt(13) + 2;// 2到14的随机数,14表示A
player[i] = random.nextInt(13) + 2;// 2到14的随机数,14表示A
}
int result = Judge(player, computer);
Show(player, computer, result);
}
// 比较
private static int Judge(int[] player, int[] computer) {
int p = 0, c = 0;
for (int i = 0; i 5; i++) {
if (14 == player[i])
p++;
if (14 == computer[i])
c++;
}
if (p 0 0 == c)
return 1;
if (p == 0 c 0)
return 2;
return 0;
}
private static void Show(int[] player, int[] computer, int result) {
System.out.print(“Your cards are:”);
for (int i = 0; i 5; i++) {
PrintCard(player[i]);
}
System.out.print(“\nThe computer’s cards are:”);
for (int i = 0; i 5; i++) {
PrintCard(computer[i]);
}
switch (result) {
case 0:
System.out.print(“\nYou and the computer are tied for the highest single card.”);
break;
case 1:
System.out.print(“\nYou has the highest single card.”);
break;
case 2:
System.out.print(“\nThe computer has the highest single card.”);
break;
}
}
private static void PrintCard(int card) {
switch (card) {
case 11:
System.out.print(“J “);
break;
case 12:
System.out.print(“Q “);
break;
case 13:
System.out.print(“K “);
break;
case 14:
System.out.print(“A “);
break;
default:
System.out.print(card + ” “);
}
}
void m() {
int[] personCard = new int[5];
int[] computerCard = new int[5];
int personCount = 0;
int computerCount = 0;
System.out.print(“Your cards are: “);
for (int i = 0; i 5; i++) {
personCard[i] = (int) (Math.random() * 13) + 2;
if (personCard[i] == 11) {
System.out.print(“J” + “\t”);
} else if (personCard[i] == 12) {
System.out.print(“Q” + “\t”);
} else if (personCard[i] == 13) {
System.out.print(“K” + “\t”);
} else if (personCard[i] == 14) {
personCount++;
System.out.print(“A” + “\t”);
} else {
System.out.print(personCard[i] + “\t”);
}
}
System.out.println(“\n”);
System.out.print(“The computer’s cards are:”);
for (int i = 0; i 5; i++) {
computerCard[i] = (int) (Math.random() * 13) + 1;
if (computerCard[i] == 11) {
System.out.print(“J” + “\t”);
} else if (computerCard[i] == 12) {
System.out.print(“Q” + “\t”);
} else if (computerCard[i] == 13) {
System.out.print(“K” + “\t”);
} else if (computerCard[i] == 14) {
computerCount++;
System.out.print(“A” + “\t”);
} else {
System.out.print(computerCard[i] + “\t”);
}
}
System.out.println(“\n”);
if (personCount 0 computerCount == 0) {
System.out.println(“You has the highest single card”);
} else if (personCount == 0 computerCount 0) {
System.out.println(“The computer has the highest single card”);
} else if (personCount == 0 computerCount == 0) {
System.out.println(“You and the computer are tied for the highest single card”);
} else if (personCount 0 computerCount 0) {
System.out.println(“Both you and the computer have the highest single card”);
}
}
}
java 随即抽扑克的问题
import java.util.ArrayList;
public class PokerPool {
private Poker[] pokers=new Poker[13*4+2];
public PokerPool(){
for(int i=0; ipokers.length; i++){
pokers[i]=new Poker(i/4,i%4);
}
}
public Poker[] getRandomPoker(int size){
size=sizepokers.length?pokers.length:size1?1:size;
ArrayListPoker ps=new ArrayListPoker();
while(ps.size()size){
Poker p=pokers[(int)(Math.random()*pokers.length)];
if(!ps.contains(p))
ps.add(p);
}
return ps.toArray(new Poker[]{});
}
public static void main(String[] args) {
PokerPool pp=new PokerPool();
Poker[] ps=pp.getRandomPoker(5);
for(int i=0; ips.length; i++)
System.out.println(ps[i]);
}
public static class Poker{
private static final String fs=”心方花黑大小”;
private static final String vs=”A234567890JQK王”;
private int value;
private int color;
public Poker(int v,int c){value=v;color=c;}
public String toString(){
return fs.charAt(color+(value12?4:0))+””+vs.charAt(value);
}
}
}
java作业:随机出现一张扑克牌。包括花色和点数。【不算大王和小王】怎么写
public class PokerTest {
public static void main(String[] args) {
Poker.show();
}
}
class Poker {
public int num;
public String breed;
public Poker() {
}
public static void show() {
int i = (int) (Math.random() * 13) + 1;// 生成1-13的随机数
String[] s = new String[] { “红心”, “黑桃”, “方块”, “黑花” };
int j = (int) (Math.random() * 4);
Poker p = new Poker();
p.num = i;
p.breed = s[j];
System.out.println(p.toString());
}
@Override
public String toString() {
return “Poker [num=” + num + “, breed=” + breed + “]”;
}
}