Java比赛练习题 2022 -4

皖北流行一种叫做“干瞪眼”的扑克牌游戏,使用的扑克牌牌面数值包括:A(1),2,3,4,5,6,7,8,9,T(10),J(11),Q(12),K(13)。这里10用T替换,暂时不考虑大鬼和小鬼。

两手牌的大小规则如下(暂不考虑其他规则):

a)单牌:只有两张牌刚好大一个点时才能进行比较,比较顺序为:A>K>Q>J>T>9>8>7>6>5>4>3。

比如:6大于5,但是不比4大,6和4不能比较。单牌2属于特殊牌,可以和其他所有普通单牌比较,并且是最大的。

b) 对子:即两张牌的点数相同,规则和单牌相似,两个2是特殊对子,可以大于所有的其他对子。注意:对子和单牌不能进行比较。

c) 炸弹:3个点数相同的牌。炸弹可以大于任何单张和对子,炸弹之间的比较与单牌和对子不同,只要满足:222>AAA>KKK>QQQ>JJJ>TTT>…>333的规则的即可。即222是最大的,AAA可以大于KKK,也可以大于333。

编写一个方法实现两手牌的比较:

int compareCards (String firstCards, String secondCards)

参数:firstCards是需要比较的第一手牌,secondCards是需要比较的第二手牌

返回:int型数据,如果firstCards和secondCards一样大,返回0;如果firstCards大于secondCards,返回1;如果firstCards小于secondCards,返回-1;如果firstCards和secondCards无法比较,返回2。

输入说明:要比较的两手牌,中间以空格隔开。每一手牌可能是任何大小的单牌、对子或炸弹。不符合规则的输入情况不需考虑。

输出说明:两手牌的比较结果

输入时两手牌之间用空格隔开。
输入样例1:J T
输入样例2:J 8

输出样例1:1
输出样例2:2

package _2022Ti;

import javax.persistence.criteria.CriteriaBuilder;
import javax.print.DocFlavor;
import java.util.Scanner;

public class Test4 {
public static int cardSize = 0;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
cardSize = st.length();


String[] s = st.split(" ");


System.out.println(compareCards(s[0], s[1]));
}

public static int compareCards(String firstCards, String secondCards) {

//有炸弹情况
if (firstCards.length() !=3 && secondCards.length()!=3) {




//单牌情况



if (cardSize == 3) {

// 判断相等情况
if (firstCards.equals(secondCards)) {
return 0;
}

if (firstCards.equals("2")) {
return 1;
}
if (secondCards.equals("2")) {
return -1;
}


// 转换数值
//相差一个点可以比较
if (firstCards.equals("J")) {
firstCards = "11";
} else if (secondCards.equals("J")) {
secondCards = "11";
}

if (firstCards.equals("Q")) {
firstCards = "12";
} else if (secondCards.equals("Q")) {
secondCards = "12";
}
if (firstCards.equals("K")) {
firstCards = "13";
} else if (secondCards.equals("K")) {
secondCards = "13";
}
if (firstCards.equals("A")) {
firstCards = "14";
} else if (secondCards.equals("A")) {
secondCards = "14";
}
if (firstCards.equals("T")) {
firstCards = "10";
} else if (secondCards.equals("T")) {
secondCards = "10";
}

if (Integer.parseInt(firstCards) + 1 == Integer.parseInt(secondCards) || Integer.parseInt(secondCards) + 1 == Integer.parseInt(firstCards)) {

return Integer.parseInt(firstCards) > Integer.parseInt(secondCards) ? 1 : -1;


} else {
return 2;
}

}
// 对子情况
//
if (cardSize == 5) {

//相等
if (firstCards.equals(secondCards)) {
return 0;
}

if (firstCards.equals("22")) {
return 1;
}
if (secondCards.equals("22")) {
return -1;
}



// 转换数值
//相差一个点可以比较
if (firstCards.equals("JJ")) {
firstCards = "1111";
} else if (secondCards.equals("JJ")) {
secondCards = "1111";
}

if (firstCards.equals("QQ")) {
firstCards = "1212";
} else if (secondCards.equals("QQ")) {
secondCards = "1212";
}
if (firstCards.equals("KK")) {
firstCards = "1313";
} else if (secondCards.equals("KK")) {
secondCards = "1313";
}
if (firstCards.equals("AA")) {
firstCards = "1414";
} else if (secondCards.equals("AA")) {
secondCards = "1414";
}
if (firstCards.equals("TT")) {
firstCards = "1010";
} else if (secondCards.equals("TT")) {
secondCards = "1010";
}

// 判断牌数 为 10 以上的
if (firstCards.length() == 4 && secondCards.length() == 4) {
int a = Integer.parseInt(firstCards.substring(0, 2));
int b = Integer.parseInt(secondCards.substring(0, 2));
if (a + 1 == b || b + 1 == a) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}

}

}

// 判断牌数其中有一个是10 以上的 也就 99 TT 1010这种

if (firstCards.length() == 4 || secondCards.length() == 4) {
if (firstCards.length() == 4) {
return 1;
}
if (secondCards.length() == 4) {
return -1;
}
}

//都是单拍 11 - 99 情况
if (firstCards.length() == 2 && secondCards.length() == 2) {
int a = Integer.parseInt(firstCards.substring(0, 1));
int b = Integer.parseInt(secondCards.substring(0, 1));
if (a + 1 == b || b + 1 == a) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
}

}
}
//有炸弹
}else{
if (firstCards.equals(secondCards)){
return 0;
}

if (firstCards.length() ==3 && secondCards.length() !=3){
return 1;

}

if (secondCards.length() ==3 && firstCards.length() != 3){
return -1;
}

if (firstCards.length() ==3 && secondCards.length() ==3){
// 转换数值
//相差一个点可以比较
if (firstCards.equals("JJJ")) {
firstCards = "111111";
} else if (secondCards.equals("JJJ")) {
secondCards = "111111";
}

if (firstCards.equals("QQQ")) {
firstCards = "121212";
} else if (secondCards.equals("QQQ")) {
secondCards = "121212";
}
if (firstCards.equals("KKK")) {
firstCards = "131313";
} else if (secondCards.equals("KKK")) {
secondCards = "131313";
}
if (firstCards.equals("AAA")) {
firstCards = "141414";
} else if (secondCards.equals("AAA")) {
secondCards = "141414";
}
if (firstCards.equals("TTT")) {
firstCards = "101010";
} else if (secondCards.equals("TTT")) {
secondCards = "101010";
}

int a=Integer.parseInt(firstCards);
int b = Integer.parseInt(secondCards);

if (a> b){
return 1;
}
if (a<b){
return -1;
}


}


}

return 0;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值