当前位置:文档之家› Java 控制台游戏(砸金花)

Java 控制台游戏(砸金花)

Java 控制台游戏(砸金花)
Java 控制台游戏(砸金花)

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();

for(int i=0;i

System.out.print(c[i]+" ");

for(int j=0;j

System.out.print(a[i][j]+" ");

}

System.out.println();

}

System.out.println();

Scanner sh = new Scanner(System.in);

while(true){

System.out.println("~~~温馨提示:“avg”为查询平均值,“sort”为列出排序,“sum”为查询总分,“get”统计学生课程成绩~~~");

System.out.println("欢迎来到学生成绩查询系统,请输入查询命令:");

String g = sh.next();

if("avg".equals(g)){

System.out.println("你选择了查询平均值命令,输入你的命令");

String h =sh.next();

avg(h);

}

else if("sort".equals(g)){

System.out.println("你选择了排序,sum为每个学生的排序,输入课程名为课程成绩排序");

String h = sh.next();

sort(h);

}

else if("sum".equals(g)){

System.out.println("你选择了查询总分命令,输入你的命令");

String h = sh.next();

sum(h);

}

else if ("get".equals(g)){

System.out.println("请输入课程号和姓名");

String h = sh.next();

String m = sh.next();

get(h,m);

}

else if("quit".equals(g)){

System.out.println("你已经成功退出!");

break;

}

else System.out.println("你输入的命令错误,请重新输入:");

}

}

public static void avg(String sh){

int index = -1;

int ledex = -1;

for(int i=0;i

if(b[i].equals(sh)){

index =i;

}

}

for(int i=0;i

if(c[i].equals(sh)){

ledex = i;

}

}

if(index==-1 && ledex==-1){

System.out.println("你输入的课程名或姓名有错误,请检查后再输入!!"); }

else if(ledex==-1){

double avg=0.0;

for(int i=0;i

avg=avg+a[index][i];

}

avg=avg/c.length;

System.out.println(sh+"的平均成绩为:"+avg);

}

else if(index==-1){

double avg=0.0;

for(int i=0;i

avg = avg+a[i][ledex];

}

avg=avg/b.length;

System.out.println(sh+"的平均成绩为:"+avg);

}

}

public static void sort(String h){

/* int sum = 0;

int max = 0;

for(int i=0;i

if(b[i].equals(h)){

for(int j=0;j

if(a[j][i]>max){

max=a[j][i];

System.out.println((j+1)+" "+max+" "+c[j]); }

}

}

}

System.out.println();

*/

int[] grade = new int[a.length];

int mm = 0;

if(h.equals("sum")){

for(int i=0;i

mm=0;

for(int j=0;j

mm=mm+a[i][j];

}

grade[i]=mm;

}

}

else{

int index=-1;

for(int i=0;i

if(b[i].equals(h)){

index=i;

}

}

for(int i=0;i

mm=0;

for(int j=0;j

mm=a[j][index];

grade[j]=mm;

}

// grade[index]=mm;

}

}

String[] ccopy =new String[c.length];

System.arraycopy(c, 0, ccopy, 0, c.length);

//Arrays.sort(grade);

//for(int i=a.length-1;i>=0;i--){

//}

for(int i=0;i

for(int j=i+1;j

if(grade[j]>grade[i]){

int t=grade[i];

grade[i]=grade[j];

grade[j]=t;

String p=ccopy[i];

ccopy[i]=ccopy[j];

ccopy[j]=p;

}

}

}

// int gg=1;

System.out.println("名次\t学生\t成绩");

for(int i=0;i

System.out.println((i+1)+"\t"+ccopy[i]+"\t"+grade[i]); }

}

public static void sum(String h){

int index = -1;

int ledex = -1;

for(int i=0;i

if(b[i].equals(h)){

index = i;

}

}

for(int i=0;i

if(c[i].equals(h)){

ledex = i;

}

}

if(index==-1){

int sum=0;

for(int i=0;i

sum=sum+a[i][ledex];

}

System.out.println(h+"的总成绩为:"+sum);

}

else if(ledex==-1){

int sum=0;

for(int i=0;i

sum=sum+a[index][i];

}

System.out.println(h+"的总成绩为:"+sum);

}

}

public static void get(String s,String h){

int index = -1;

int ledex = -1;

for(int i=0;i

if(b[i].equals(s)){

index = i;

}

}

for(int i=0;i

if(c[i].equals(h)){

ledex = i;

}

}

if(index!=-1 && ledex!=-1){

System.out.println(a[ledex][index]);

}

}

}

//学生成绩

酒店管理Hotel Manage

import java.util.Scanner;;

public class HotelManger {

public static void main(String[] args) {

String [][] rooms = new String[15][20];

Scanner sh = new Scanner(System.in);

for(int i=0;i

for(int j=0;j

rooms[i][j]="null";

}

}

while(true){

System.out.println("欢迎来到***酒店,请入住"); String a = sh.next();

if(a.equals("search")){

search(rooms);

}

else if(a.equals("in")){

System.out.println("请输入你的房号和姓名:");

int n = sh.nextInt();

String m = sh.next();

in(n,m,rooms);

}

else if(a.equals("out")){

int n=sh.nextInt();

out(n,rooms);

}

else if(a.equals("quit")){

System.out.println("你已经成功退出!!!");

break;

//quit();

}

else {

System.out.println("你输入的编码有错误,请再次输入!");

}

}

}

public static void search(String[][] rooms){

String temp = " ";

for(int i=0;i

for(int j=0;j

if(i<9){

System.out.print("0"+(i+1));

if(j<9) System.out.print("0"+(j+1)+" ");

else System.out.print((j+1)+" ");

}

else {

System.out.print(i+1);

if(j<9) System.out.print("0"+(j+1)+" ");

else System.out.print((j+1)+" ");

}

// System.out.print(rooms[i][j]+" ");

};

System.out.println();

for(int j=0;j

System.out.print(rooms[i][j]+" ");

}

System.out.println();

System.out.println();

}

}

public static void in(int number,String name,String[][] rooms){

相关主题
文本预览
相关文档 最新文档