运用早期知识完成作业,不足之处希望大家指正

来源:7-1 简易扑克牌游戏

苦天昆吾

2019-05-07 11:06

package poke;


import java.util.Scanner;


public class gameOn {


public static void main(String[] args) {


// 建立牌组,同时赋值,同位置一一对应。

int[] pokeValue = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,

51 };

String[] pokeFace = { "方块2", "方块3", "方块4", "方块5", "方块6", "方块7", "方块8", "方块9", "方块10", "方块J", "方块Q", "方块K",

"方块A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K", "梅花A",

"红心2", "红心3", "红心4", "红心5", "红心6", "红心7", "红心8", "红心9", "红心10", "红心J", "红心Q", "红心K", "红心A", "黑桃2",

"黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", "黑桃A" };

Scanner input = new Scanner(System.in);

System.out.println("玩家入场");

System.out.println("请输入1号玩家姓名:");

String cusName1 = input.next();

System.out.println("请输入2号玩家姓名:");

String cusName2 = input.next();

// System.out.println("---------------洗牌前---------------");

// for (int i = 0; i < pokeFace.length; i++) {

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

// }    洗牌前牌组

System.out.println("");

System.out.println("---------------洗牌huahuahua.....Loading-------------");

for (int j = 0; j < pokeFace.length; j++) {

int a = (int) (Math.random() * pokeFace.length);

String temStr;

temStr = pokeFace[j];

pokeFace[j] = pokeFace[a];

pokeFace[a] = temStr;

int temInt;

temInt = pokeValue[j];

pokeValue[j] = pokeValue[a];

pokeValue[a] = temInt;

}

// for (int i = 0; i < pokeFace.length; i++) {

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

// }  洗牌后牌组


//检测洗牌后效果 System.out.println(pokeFace[3]+"  "+pokeValue[3]);

// 存储随机数,作为手牌

int[] cardNum = new int[4];


for (int k = 0; k < 4; k++) {

int b = (int) (Math.random() * pokeFace.length);

cardNum[k] = b;

for (int l = 0; l < k; l++) {

if (b == cardNum[l]) {

k--;

break;

}

}


}


// for(int i=0;i<cardNum.length;i++) {

// System.out.println(cardNum[i]);

// }  //测试抽排合理性

System.out.print(cusName1 + "的手牌为:");

System.out.print(pokeFace[cardNum[0]] + "  " + pokeFace[cardNum[1]]);

System.out.println("");

System.out.print(cusName2 + "的手牌为:");

System.out.print(pokeFace[cardNum[2]] + "  " + pokeFace[cardNum[3]]);

System.out.println("");

int temCard = 0;

int p = 0;

for (int i = 0; i < cardNum.length; i++) {

if (temCard < pokeValue[cardNum[i]]) {

temCard = pokeValue[cardNum[i]];

p = i;

}

}

if (p < 2) {

System.out.println("胜者为" + cusName1);

} else {

System.out.println("胜者为" + cusName2);

}


input.close();

}


}


写回答 关注

3回答

  • AinyG
    2019-05-08 11:30:53

    你这样的话,举个例子,红桃7,8岂不是比梅花J,Q大???

    苦天昆吾

    int[] pokeValue = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41,45, 49, 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51 }; 设置初始值错误了,已改正

    2019-05-08 14:02:08

    共 1 条回复 >

  • tiger爱小狄
    2019-05-08 09:56:20

    你这没按题目要求来啊,要求一人发一张,先按点数比大小,点数一样,再按花色比。

    tiger爱... 回复苦天昆吾

    能写出来已经很厉害了,有这个逻辑和意识,年薪百万的节奏啊!

    2019-05-09 15:33:28

    共 2 条回复 >

  • 慕的地2092416
    2019-05-07 22:38:08

    nb 刚学都看不太懂

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题