标题: 炸金花案例 [打印本页] 作者: helloxiaoyu 时间: 2015-8-7 23:01 标题: 炸金花案例 import java.util.Random;
class CardTest{
public static void main(String[] args){
FaPaiJi fpj = new FaPaiJi();
Card c0 = fpj.getCard();
Card c1 = fpj.getCard();
Card c2 = fpj.getCard();
Card[] cards = new Card[3];
cards[0] = c0;
cards[1] = c1;
cards[2] = c2;
for( int x=0; x<cards.length; x++){
System.out.println(cards[x].getHuase()+""+cards[x].getDianshu()+"");
}
}
}
class Card {
//成员变量
String huase;//花色
int dianshu;//点数
//构造方法
Card(){}
Card(String huase, int dianshu){
this.huase = huase;
this.dianshu = dianshu;
}
public String getHuase(){
return huase;
}
public int getDianshu(){
return dianshu;
}
}
class FaPaiJi {
// 成员方法
// 返回值类型
// 参数列表
public Card getCard(){
//创建对象 String[] hs = {"黑桃","梅花","方块","红桃"};
Random r = new Random();
int x = r.nextInt(4);
String huase = hs[x];
Card card = new Card(huase,dianshu);
return card;
}
}
输出结果可能出现相同项,怎样解决这个问题?
/*
生成牌的时候,直接生成52不一样的牌
随机 Random().nextInt(52),返回的是 0--51
只要随机数不一样
*/ Random r = new Random();
int x1 = r.nextInt(13)+1;
int x2 = r.nextInt(13)+14;
int x3 = r.nextInt(13)+27;
int x4 = r.nextInt(13)+40;
String[][] hs = {"黑桃","梅花","方块","红桃"};
后面怎么做?求解
public class D {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10","J", "Q", "K" };
for (String color : colors) {
for (String number : numbers) {
array.add(color.concat(number));
}
}
// 洗牌
Collections.shuffle(array);
for (int i = 0; i < 3; i++) {
System.out.println(array.get(i));
}
}
} 作者: 龙哥Longer 时间: 2015-8-20 22:14
谢谢分享,学习了作者: helloxiaoyu 时间: 2015-8-20 22:25
import java.util.Random;
public class C {
public static void main(String[] args) {
Random random = new Random();
int a[] = new int[4];
for (int i = 0; i < a.length; i++) {
//生成一个介于1到52的数字
a[i] = random.nextInt(52)+1;
for (int j = 1; j < i; j++) {
//如果重复,退回去重新生成随机数
while (a[i] == a[j]) {
i--;
}
}
}
for (int i = 1; i < a.length; i++) {
if (a[i]<14&&a[i]>0) {
System.out.println("红桃"+a[i] +" ");
}else if (a[i]<27&&a[i]>13) {
System.out.println("方块"+(a[i]-13) +" ");
}else if (a[i]<40&&a[i]>26) {
System.out.println("黑桃"+(a[i]-26) +" ");
}else {
System.out.println("梅花"+(a[i]-39) +" ");
}
}