There are 13 playing cards, how to determine if there is a straight in the cards (Straight: 5 consecutive cards)

There are 13 playing cards, how to determine if there is a straight in the cards (Straight: 5 consecutive cards)

The analysis ideas are as follows:

1. The cards that may appear are numbers (2-10), (JK, A, King size), first replace the cards that cannot be directly arranged as numbers with numbers;

2. Convert the entire array to a number type;

3. Sort the array from small to large;

4. Make a judgment. When the number of consecutive cards in the array is greater than or equal to 5 (that is, the next card minus the number of the previous card reaches 4 consecutive groups), it is judged as a straight.

The specific code is as follows:

package calculate;

public class Poker {

	public static void main(String[] args) {
		
		String[] poker=new String[] {"3","5","6","A","K","5","J","2","5","7","2","8","10"};
		int[] num=new int[poker.length];
		//将J,Q,K,A,2,大王,小王都转成数字
		for(int i=0;i<poker.length;i++) {
			if(poker[i]=="J") {
				poker[i]="11";
			}
			else if(poker[i]=="Q") {
				poker[i]="12";
			}
			else if(poker[i]=="K") {
				poker[i]="13";
			}
			else if(poker[i]=="A") {
				poker[i]="14";
			}
			else if(poker[i]=="2") {
				poker[i]="15";
			}
			else if(poker[i]=="minKing") {
				poker[i]="0";
			}
			else if(poker[i]=="maxKing") {
				poker[i]="0";
			}
		}
		//将字符串数组转变为数字数组
		for(int i=0;i<poker.length;i++) {
			num[i]=Integer.valueOf(poker[i]);
		}

		//将数组排序
		for(int i=0;i<num.length;i++) {
			for(int j=0;j<num.length-i-1;j++) {
				if(num[j]>num[j+1]) {
					int temp=num[j];
					num[j]=num[j+1];
					num[j+1]=temp;
				}
				
			}
		}
		//当数组中后一位减前一位数字=1连续超过4组,则判断有顺子,否则没有
		int count=1;
		for(int i=0;i<num.length-1;i++) {
			if(num[i+1]-num[i]==1) {
				count++;
				if(count>=4) {
					System.out.println("有顺子");
				}
				else {
					System.out.println("没有顺子");
				}
			}
			else if(num[i+1]-num[i]!=1) {
				count=0;
			}
			
		}
	}

}

 

Guess you like

Origin blog.csdn.net/dream_18/article/details/115356035