跪求高手帮忙写个java程序

2024-12-19 17:39:44
推荐回答(2个)
回答1:

有意思,我就给你设计下
import java.util.*;
import java.io.*;

class Poker{
private String color;
private String value;
private int inner;
/*inner值是:2-14*/
public Poker(String color,int inner){
this.color=color;
this.inner=inner;
if(inner==11) value="J";
else if(inner==12) value="Q";
else if(inner==13) value="K";
else if(inner==14) value="A";
else value=String.valueOf(inner+1);
this.value=value;
}
public String getColor(){
return this.color;
}
public String getValue(){
return this.value;
}
public int getInner(){
return this.inner;
}
public String getPoker(){
return this.color+this.value;
}
}
class Game{
private Poker[] pokers=new Poker[52];
private Poker[] random;
private String type;
private int level;
public Game(){
for(int i=0;i if(i/13==0) {
pokers[i]=new Poker("红桃",i%13+2);
}
if(i/13==1) {
pokers[i]=new Poker("黑桃",i%13+2);
}
if(i/13==2) {
pokers[i]=new Poker("方片",i%13+2);

}
if(i/13==3) {
pokers[i]=new Poker("樱花",i%13+2);

}
}
}
public void print(){
for(int i=0;i System.out.println(pokers[i].getPoker());
}
}
public void printSelect(){
System.out.println("-------"+type+"--------");
for(int i=0;i System.out.println(random[i].getPoker());
}
System.out.println("-------------------------------");
}
public void randomSelect(int num,boolean ai){
Set set=new HashSet();
if(!ai){

Scanner s=new Scanner(System.in);
for(int i=0;i System.out.println("请输入牌序号(1-52)[当前第"+(i+1)+"张,共"+num+"张]:");
int tmp=s.nextInt();
if(tmp>52 || tmp<1){
System.out.println("错误的序列!");
i--;
continue;
}
if(set.contains(tmp)){
System.out.println("该扑克牌已选取!");
i--;
continue;
}
set.add(i-1);
}
s.close();
}
else{
for(int i=0;i int tmp=(int)(Math.random()*52);
if(set.contains(tmp)){
i--;
continue;
}
else set.add(i);
}
}
random=new Poker[num];
int m=0;
for(int option:set){
random[m++]=pokers[option];
}
}
public void ruffle(){
for(int i=0;i<52;i++){
int a=(int)(Math.random()*52);
int b=(int)(Math.random()*52);
Poker temp;
temp=pokers[a];
pokers[a]=pokers[b];
pokers[b]=temp;
}
}
//检查是否同花
public boolean isSameColor(){
Set set=new HashSet();
for(int i=0;i set.add(random[i].getColor());
if(set.size()==1) return true;
else return false;
}
//检查是否顺子
public boolean isStraight(){
int[] list=new int[random.length];
for(int i=0;i list[i]=random[i].getInner();
Arrays.sort(list);
for(int i=1;i if(list[i-1]!=list[i]-1) return false;
return true;
}
//检查同花顺
public boolean isSameColorAndStraight(){
if(isSameColor() && isStraight()) return true;
else return false;
}
//最大同花顺A K Q J 10
public boolean isMax(){
if(isSameColorAndStraight()){
int[] list=new int[random.length];
for(int i=0;i list[i]=random[i].getInner();
Arrays.sort(list);
if(list[0]==10 && list[4]==14) return true;
else return false;
}
return false;
}
//检查是否4个一样的
public boolean isBomb(){
Map map=new HashMap();
for(int i=0;i if(map.containsKey(random[i].getInner())) map.put(random[i].getInner(),map.get(random[i].getInner())+1);
else map.put(random[i].getInner(),1);
for(int value:map.keySet()){
if(map.get(value)==4) return true;
}
return false;
}
//检查是否3+2
public boolean isThreeAndTwo(){
Map map=new HashMap();
for(int i=0;i if(map.containsKey(random[i].getInner())) map.put(random[i].getInner(),map.get(random[i].getInner())+1);
else map.put(random[i].getInner(),1);
if(map.size()!=2) return false;
else{
for(int value:map.keySet()){
if(map.get(value)==2 || map.get(value)==3) return true;
else return false;
}
return false;
}
}
//检查有3个一样的
public boolean isThree(){
Map map=new HashMap();
for(int i=0;i if(map.containsKey(random[i].getInner())) map.put(random[i].getInner(),map.get(random[i].getInner())+1);
else map.put(random[i].getInner(),1);
for(int value:map.keySet()){
if(map.get(value)==3) return true;
}
return false;
}
//检查有2个一样的(单对)
public boolean isTwoByOdd(){
Map map=new HashMap();
for(int i=0;i if(map.containsKey(random[i].getInner())) map.put(random[i].getInner(),map.get(random[i].getInner())+1);
else map.put(random[i].getInner(),1);
for(int value:map.keySet()){
if(map.get(value)==2) return true;
}
return false;
}
//检查有2个一样的(双对)
public boolean isTwoByDouble(){
Map map=new HashMap();
int number=0;
for(int i=0;i if(map.containsKey(random[i].getInner())) map.put(random[i].getInner(),map.get(random[i].getInner())+1);
else map.put(random[i].getInner(),1);
for(int value:map.keySet()){
if(map.get(value)==2) number++;
}
if(number==2) return true;
else return false;
}
public void check(){
if(isTwoByOdd()) {type="你手中的牌是单对子";level=1;}
else if(isTwoByOdd()) {type="你手中的牌是双对子";level=2;}
else if(isThree()) {type="你手中的牌是小炸弹";level=3;}
else if(isThreeAndTwo()) {type="你手中的牌是小炸弹加单对子";level=4;}
else if(isBomb()) {type="你手中的牌是核弹";level=7;}
else if(isMax()) {type="恭喜你,你手中的牌是同花大顺";level=9;}
else if(isSameColorAndStraight()) {type="恭喜你,你手中的牌是同花顺";level=8;}
else if(isStraight()) {type="你手中的牌是顺子";level=5;}
else if(isSameColor()) {type="你手中的牌是同花";level=6;}
else {type="你手中的牌比较小";level=0;}
}
public String getType(){
return this.type;
}
public int getLevel(){
return this.level;
}
public void clear(){
random=new Poker[5];
type="";
level=0;
}
public static void main(String[] str){
Game game=new Game();
game.ruffle();
for(int i=0;i<5000;i++){
game.clear();
game.ruffle();
game.randomSelect(5,true);
game.check();
//选择5000次,看看有多少顺子
if(game.getLevel()==5) game.printSelect();
}
}
}

回答2:

学习