求解一个有关字符串的发牌问题 求解把52张牌发给四个人,判断其中一个人的牌中是否有:一对,两对,三张同点的牌、五张点数连续的牌、三张同点加一对的牌,并输出,,,哪位高手给个代码,万分感谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PlayingCard1 extends JFrame { private String playerC[],playerH[]; private JButton btu1,btu2; private int count = 0; private String s =""; final String faces[] = {"Ace","Deuce","Three","Four","Five","Six","Seven", "Eight","Nine","Ten","Jack","Queen","King"}; final String suits[] = {"Hearts","Diamonds","Clubs","Spades"}; public PlayingCard1() { playerC = new String[ 13 ]; playerH = new String[ 4 ]; btu1 = new JButton("Deal the Card"); btu2 = new JButton("Shuffle the Card"); btu1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ for(int w=0;w<playerC.length;w++) { int i = (int)( Math.random()*13 ); int j = (int)( Math.random()*4 ); playerC[count] = faces[i]; playerH[count%4] = "," + suits[i%4]; count++; } deal(); count = 0; } } ); /* btu2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ int sec = (int)(Math.random()*52); } } ); */ getContentPane().add(btu1); setLocation(300,300); setSize(300,300); setVisible(true); } public void check() { int dui = 0; for(int r=0;r<playerC.length;r++){ for(int y=0;y<faces.length;y++){ if ( playerC[y].equals(faces[r]) ){ dui++; if (dui>1) s += playerC[y] + ":" + dui ; } } dui = 0; } } public void deal() { for(int u=0;u<playerC.length;u++) s += playerC[u] + playerH[u%4] + "\n"; check(); JOptionPane.showMessageDialog(null,s,"Information to show",JOptionPane.INFORMATION_MESSAGE); s = ""; } public static void main(String[] args) { PlayingCard1 application = new PlayingCard1(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}大家看看怎么样 个人觉得final String faces[] = {"Ace","Deuce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};final String suits[] = {"Hearts","Diamonds","Clubs","Spades"};这样定义不是太好,因为判断重复的时候很麻烦。所以建议,例如:final String[] heartsFaces = {"H.A","H.2","H.3","H.4"......"H.J","H.Q","H.K"};其他花色的也同样。每次发牌都随机从数组中移除,这样避免出现重复的牌。 eclipse java连接oracle数据库问题 变量的简单问题,很菜 jFreechar 的下载 java applet数字签名 如何设置Swing控件不可用(即setEnabled(false))时的字体颜色?需要一个通用方法,急呀!!!!!! 帮我解释个名词,谢谢了!必给分! 晕晕晕!!! 这个问题好怪。 Java语法问题 java 实例变量存放在堆区还是栈区? 方法参数及返回值的问题 好吧,大家来优化程序啦
import java.awt.event.*;
import javax.swing.*;public class PlayingCard1 extends JFrame {
private String playerC[],playerH[];
private JButton btu1,btu2;
private int count = 0;
private String s ="";
final String faces[] = {"Ace","Deuce","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King"};
final String suits[] = {"Hearts","Diamonds","Clubs","Spades"};
public PlayingCard1() {
playerC = new String[ 13 ];
playerH = new String[ 4 ];
btu1 = new JButton("Deal the Card");
btu2 = new JButton("Shuffle the Card");
btu1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int w=0;w<playerC.length;w++) {
int i = (int)( Math.random()*13 );
int j = (int)( Math.random()*4 );
playerC[count] = faces[i];
playerH[count%4] = "," + suits[i%4];
count++;
}
deal();
count = 0;
}
}
);
/* btu2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int sec = (int)(Math.random()*52);
}
}
); */
getContentPane().add(btu1);
setLocation(300,300);
setSize(300,300);
setVisible(true);
}
public void check() {
int dui = 0;
for(int r=0;r<playerC.length;r++){
for(int y=0;y<faces.length;y++){
if ( playerC[y].equals(faces[r]) ){
dui++;
if (dui>1)
s += playerC[y] + ":" + dui ;
}
}
dui = 0;
}
}
public void deal() {
for(int u=0;u<playerC.length;u++)
s += playerC[u] + playerH[u%4] + "\n";
check();
JOptionPane.showMessageDialog(null,s,"Information to show",JOptionPane.INFORMATION_MESSAGE);
s = "";
}
public static void main(String[] args) {
PlayingCard1 application = new PlayingCard1();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
大家看看怎么样
final String faces[] = {"Ace","Deuce","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King"};
final String suits[] = {"Hearts","Diamonds","Clubs","Spades"};
这样定义不是太好,因为判断重复的时候很麻烦。
所以建议,例如:
final String[] heartsFaces = {"H.A","H.2","H.3","H.4"......"H.J","H.Q","H.K"};
其他花色的也同样。
每次发牌都随机从数组中移除,这样避免出现重复的牌。