开发软件:idea
1.主函数内容
public static void main(String[] args) {
System.out.println("欢迎光临");
Scanner s = new Scanner(System.in);
System.out.println("请输入总数字:");
userAMoney = s.nextInt();
userBMoney = userAMoney;
int currentMoney = 0;
while (userAMoney > 0 && userBMoney > 0) {
System.out.println("请输入本局数字:");
currentMoney = s.nextInt();
if (currentMoney <= 0 || currentMoney > userAMoney || currentMoney > userBMoney)
{
System.out.println("电脑或你的数字不足,请重新输入!");
continue;
}
dispatchCards();
printCards(twoHandsCards);
if (compare() > 0) {
userAMoney += currentMoney;
userBMoney -= currentMoney;
System.out.println("玩家获胜!");
} else if (compare() < 0) {
userAMoney -= currentMoney;
userBMoney += currentMoney;
System.out.println("电脑获胜!");
} else {
System.out.println("平局!");
}
System.out.println("玩家数字:" + userAMoney + ";电脑数字:" + userBMoney);
}
if (userAMoney <= 0) {
System.out.println("你输光数字了");
} else {
System.out.println("电脑已经输光数字了");
}
}
2.下面是调牌的方法
public static void dispatchCards() {
int i = 0;
while (i < twoHandsCards.length) {
int h = (int) (Math.random() * 4) + 1;
int d = (int) (Math.random() * 13) + 2;
int j = 0;
for (; j < i; j++) {
if (twoHandsCards[j] == h * 100 + d) {
break;
}
}
if (j < i) {
continue;
} else {
twoHandsCards[i++] = h * 100 + d;
}
}
}
3.下面的代码是处理扑克花色还有特殊扑克牌
public static void printCards(int[] oneHandCards) {
for (int i = 0; i < oneHandCards.length; i++) {
if (i == 0) {
System.out.print("你的牌:");
}
if (i == 3) {
System.out.print("\t电脑的牌:");
}
switch (oneHandCards[i] / 100) {
case 1:
System.out.print("♠");
break;
case 2:
System.out.print("♥");
break;
case 3:
System.out.print("♣");
break;
case 4:
System.out.print("♦");
break;
}
if (oneHandCards[i] % 100 <= 10) {
System.out.print(oneHandCards[i] % 100);
} else {
switch (oneHandCards[i] % 100) {
case 11:
System.out.print("J");
break;
case 12:
System.out.print("Q");
break;
case 13:
System.out.print("K");
break;
case 14:
System.out.print("A");
break;
}
}
System.out.print(" ");
}
System.out.println();
}
4.点数数比较
public static int compare() {
int[] aCards = new int[3];
int[] bCards = new int[3];
for (int i = 0; i < 3; i++) {
aCards[i] = twoHandsCards[i];
}
for (int i = 3; i < 6; i++) {
bCards[i - 3] = twoHandsCards[i];
}
return fromCardsToNumber(aCards) - fromCardsToNumber(bCards);
}
5.核心(可以按照自己的想法编写程序)
public static int fromCardsToNumber(int[] oneHandCards) {
for (int i = 0; i < oneHandCards.length - 1; i++) {
for (int j = i + 1; j < oneHandCards.length; j++) {
if (oneHandCards[i] % 100 < oneHandCards[j] % 100) {
int temp = oneHandCards[i];
oneHandCards[i] = oneHandCards[j];
oneHandCards[j] = temp;
}
}
}
int num = 0;
if (oneHandCards[0] % 100 == oneHandCards[1] % 100
&& oneHandCards[1] % 100 == oneHandCards[2] % 100) {
num = 6 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
&& oneHandCards[1] / 100 == oneHandCards[2] / 100
&& oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
&& oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
num = 5 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} else if (oneHandCards[0] / 100 == oneHandCards[1] / 100
&& oneHandCards[1] / 100 == oneHandCards[2] / 100) {
num = 4 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} else if (oneHandCards[0] % 100 == oneHandCards[1] % 100 + 1
&& oneHandCards[1] % 100 == oneHandCards[2] % 100 + 1) {
num = 3 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} else if (oneHandCards[0] % 100 == oneHandCards[1] % 100) {
num = 2 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} else if (oneHandCards[1] % 100 == oneHandCards[2] % 100) {
num = 2 * 1000000 + oneHandCards[1] % 100 * 10000 + oneHandCards[2]
% 100 * 100 + oneHandCards[0] % 100;
} else {
num = 1 * 1000000 + oneHandCards[0] % 100 * 10000 + oneHandCards[1]
% 100 * 100 + oneHandCards[2] % 100;
} return num;
}
整体代码参考
效果截图