public class CardGame {
static int userMoney = 0;
static int computerMoney = 0;
static int[] handcards = new int[6];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入赌本:");
userMoney = sc.nextInt();
computerMoney = userMoney;
while (userMoney > 0 && computerMoney > 0) {
System.out.println("请输入本局赌注:");
int duzhu = sc.nextInt();
if (duzhu <= 0 || duzhu > userMoney || duzhu > computerMoney) { System.out.println("赌注不正确,请重新输入:");
continue;
}
printpai();
System.out.println();
if (compara() > 0) {
System.out.println("玩家获胜!");
userMoney += duzhu;
computerMoney -= duzhu;
} else if (compara() < 0) {
System.out.println("电脑获胜!");
userMoney -= duzhu;
computerMoney += duzhu;
} else {
System.out.println("平局!");
}
System.out
.println("玩家赌本为:" + userMoney + " 电脑赌本为:" + computerMoney); }
}
public static void printpai() {
for (int j = 0; j < handcards.length; j++) {
int huase = (int) (Math.random() * 4 + 1);
int paimian = (int) (Math.random() * 13 + 2);
handcards[j] = (huase * 100 + paimian);
switch (huase) {
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 (handcards[j] % 100 <= 10) {
System.out.print(handcards[j] % 100 + " ");
} else {
switch (handcards[j] % 10) {
case 1:
System.out.print("J ");
break;
case 2:
System.out.print("Q ");
break;
case 3:
System.out.print("K ");
break;
case 4:
System.out.print("A ");
break;
}
}
}
}
// 比较两手牌时不能用数字的大小sum+(handcards[j] = (huase * 100 + paimian))
// 的加以判断,那样判断的结果会令黑桃A<小于梅花8,红桃9>黑桃Q的情况,不符合现实public static int compara() {
int[] userCard = new int[handcards.length / 2];
int[] computerCard = new int[handcards.length / 2];
// 打印数字类型的牌面值,数组
// System.out.println(Arrays.toString(handcards));
// 定义玩家的牌,取数组元素的前三个
for (int i = 0; i < userCard.length; i++) {
userCard[i] = handcards[i];
}
// 定义电脑的牌,取数组元素的后三个
for (int i = handcards.length / 2; i < handcards.length; i++) {
computerCard[i - handcards.length / 2] = handcards[i];
}
// 比较两玩家的牌的大小值
return fromCardToNumber(userCard) - fromCardToNumber(computerCard);
}
public static int fromCardToNumber(int[] temp) {
// 下面是一段冒泡排序
for (int i = 0; i < temp.length; i++) {
// 比牌面值的大小,即现实中红桃5(代表205)永远是比黑桃(K113)要小的,对牌的大小顺序进行排序
for (int j = 0; j < temp.length - i - 1; j++) {
if (temp[j] % 100 < temp[j + 1] % 100) {
int tmp = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = tmp;
}
}
// 输出冒泡排序的两组数字,前面一半的数组值代表玩家的,后一半的数组值代表式电脑的// System.out.print(temp[i] + " ");
}
// System.out.println();
int num = 0;
// 判断三条的情况
if (temp[0] % 100 == temp[1] % 100 && temp[1] % 100 == temp[2] % 100) {
num = 6 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] / 100 == temp[1] / 100
&& temp[1] / 100 == temp[2] / 100
&& temp[0] % 100 == temp[1] % 100 + 1
&& temp[1] % 100 == temp[2] % 100 + 1) {
num = 5 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] / 100 == temp[1] / 100
&& temp[1] / 100 == temp[2] / 100) {
num = 4 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] % 100 == temp[1] % 100 + 1
&& temp[1] % 100 == temp[2] % 100 + 1) {
num = 3 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
// 数组是从大到小排列的,因此在比较时要注意
} else if (temp[0] % 100 == temp[1] % 100
|| temp[1] % 100 == temp[2] % 100) {
num = 2 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else {
num = 1 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
}
return num;
}
}
--------------------------------------------------------------------------------------------------------------
/*
*
用面向对象的方式重写扑克牌游戏程序,定义以下类型:
1,花色,枚举类型,包含枚举值:黑,红,梅,方。
2,点数,枚举类型,包含枚举值:2-A
3,一张扑克牌(Card):包含属性:花色,点数
4,一手扑克牌(OneHandCards):
包含属性:Card[] cards;//三张扑克牌
方法: public int compareTo(OneHandCards c);
该方法返回某两手扑克牌大小比较的结果:
如果当前这手牌比c大,则返回>0的值
如果当前这手牌比c小,则返回<0的值
如果当前这手牌和c相等,则返回0
5,玩家类(Player) :包含属性:姓名,赌本,当前一手牌
6,游戏类(Game):包含属性:两个玩家
包含方法:void 发牌();为该游戏中的所有玩家发牌。
void startGame();开始游戏。
*/
package Leiduixiang;
import java.util.Scanner;;
public class StudentManage {
static int[][] a = {{70,67,67,78},{90,89,78,78},{98,87,89,84},{90,98,87,56}}; static String[] b = {"语文","数学","英语","化学"};
static String[] c = {"张三","张四","张五","张六"};
public static void main(String[] args) {
//int[][] a = {{70,67,67,78},{90,89,78,78},{98,87,89,84},{90,98,87,56},{90,83,78,76},{90,89,68 ,78},{80,85,78,77},{97,85,88,76}};
//String[] b = {"语文","数学","英语","化学","物理","政治","生物","地理"}; //String[] c = {"张三","张四","张五","张六","张七","张八","张九","张十"}; for(int j=0;j System.out.print(" "+b[j]); } System.out.println();