第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

有些方法還是不太會,要參照老師講的用法才能寫對~


  1. package fourth;

  2. /**

  3. ?* 撲克牌類

  4. ?* @author jiuge

  5. ?*

  6. ?*/

  7. public class poker implements Comparable<poker>{

  8. public String type; ? ?//花色

  9. public String name; ? ?//牌名

  10. public int typevalue; ?//通過兩個value方便對牌進行大小比較

  11. public int namevalue;

  12. public poker(){


  13. }

  14. public poker(String type,String name){

  15. this.type=type;

  16. this.name=name;

  17. if(type=="紅桃") this.typevalue=4;

  18. else if(type=="黑桃") this.typevalue=3;

  19. else if(type=="方塊") this.typevalue=2;

  20. else if(type=="梅花") this.typevalue=1;

  21. if(name=="J") this.namevalue=11;

  22. else if(name=="Q") this.namevalue=12;

  23. else if(name=="K") this.namevalue=13;

  24. else if(name=="A") this.namevalue=14;

  25. else this.namevalue=Integer.valueOf(name);

  26. }

  27. @Override

  28. public int compareTo(poker o) { //重寫compare方法

  29. int returnvalue = -1000;

  30. if(this.namevalue>o.namevalue)

  31. returnvalue= 1;

  32. else if(this.namevalue<o.namevalue)

  33. returnvalue= -1;

  34. else if(this.namevalue==o.namevalue){

  35. if(this.typevalue>o.typevalue)

  36. returnvalue= 1;

  37. else if(this.typevalue<o.typevalue)

  38. returnvalue= -1;

  39. else returnvalue=0;

  40. }

  41. return returnvalue;

  42. }

  43. }



  1. package fourth;


  2. public class player {

  3. String id;

  4. String name;

  5. public player(String id,String name){

  6. this.id=id;

  7. this.name=name;

  8. }

  9. }


  1. package fourth;

  2. import java.util.*;

  3. public class test {

  4. public void creat(){

  5. /***********************給牌****************************/

  6. poker[] pokers = new poker[52];

  7. String[] types={"紅桃","黑桃","方塊","梅花"};

  8. String[] names={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

  9. for(int i=0;i<4;i++){ ? ? ? ? ?//創(chuàng)建一副牌,這時的撲克牌組是有順序的

  10. for(int j=0;j<13;j++){

  11. pokers[(i*13+j)]=new poker(types[i],names[j]);

  12. }

  13. }

  14. /************************洗牌***************************/

  15. Integer[] arr=new Integer[52]; ? //建立一個52長度(一副牌的長度)的數(shù)組,將下標作為值

  16. for(int i=0;i<52;i++){

  17. arr[i]=i;

  18. }

  19. List<Integer> num=new ArrayList<Integer>();

  20. num.addAll(Arrays.asList(arr)); ?//把該數(shù)組轉換成序列后打亂順序

  21. Collections.shuffle(num);

  22. for (int i : num) { ? ? ?//把亂序的值再返回給數(shù)組,得到一個亂序的數(shù)組,且每次運行程序這個數(shù)組都不一樣

  23. arr[i]=num.get(i); ? //把這個亂序的數(shù)組作為撲克牌組的下標,能夠達到洗牌的效果

  24. }

  25. /************************發(fā)牌****************************/

  26. Scanner in=new Scanner(System.in);//準備創(chuàng)建兩名玩家

  27. System.out.print("請輸入第一位玩家的name:");

  28. String name=in.next();

  29. player player1=new player("1",name);

  30. System.out.print("請輸入第二位玩家的name:");

  31. name=in.next();

  32. player player2=new player("2",name);

  33. List<poker> p1=new ArrayList<poker>();//player1的牌組

  34. List<poker> p2=new ArrayList<poker>();//player2的牌組

  35. System.out.print("請輸入發(fā)牌數(shù)量(26張以內):");

  36. int i;

  37. do{

  38. i=in.nextInt();

  39. if(i<0||i>26) System.out.print("選擇錯誤!請重新選擇發(fā)牌數(shù)量:");

  40. }while(i<0||i>26);

  41. for(int n=0;n<2*i;n++){ ? ?//給兩名玩家交替發(fā)牌

  42. if(n%2==0){ ? ? ? ? ??

  43. p1.add(pokers[arr[n]]); ?//pokers中的對象已固定,通過亂序的數(shù)組arr作為下標,實現(xiàn)洗牌功能

  44. System.out.println("玩家"+player1.name+"發(fā)到一張牌");

  45. }

  46. else{

  47. p2.add(pokers[arr[n]]);

  48. System.out.println("玩家"+player2.name+"發(fā)到一張牌");

  49. }

  50. }

  51. System.out.println("**********發(fā)牌完畢***********");

  52. /*************************輸贏***************************/

  53. System.out.print("玩家"+player1.name+"的牌:"); //玩家一獲得的牌

  54. for(int a=0;a<p1.size();a++){ ? ? ??

  55. poker po1=p1.get(a);

  56. System.out.print(po1.type+po1.name+" ");

  57. }

  58. System.out.println();

  59. System.out.print("玩家"+player2.name+"的牌:"); //玩家二獲得的牌

  60. for(int a=0;a<p2.size();a++){

  61. poker po2=p2.get(a);

  62. System.out.print(po2.type+po2.name+" ");

  63. }

  64. System.out.println();

  65. Collections.sort(p1);//這里對牌進行排序,排序是遞增的,故最后一張牌是最大的

  66. Collections.sort(p2);//下面獲取兩名玩家各自最大的牌

  67. System.out.println("玩家"+player1.name+"最大的牌:"+p1.get((p1.size()-1)).type+p1.get((p1.size()-1)).name);

  68. System.out.println("玩家"+player2.name+"最大的牌:"+p2.get((p2.size()-1)).type+p2.get((p2.size()-1)).name);

  69. int result=p1.get((p1.size()-1)).compareTo(p2.get((p2.size()-1)));//對兩名玩家最大的牌進行比較

  70. switch(result){

  71. case -1:System.out.println("玩家"+player2.name+"贏了!");break;

  72. case 1:System.out.println("玩家"+player1.name+"贏了!");break;

  73. }

  74. in.close();

  75. }

  76. public static void main(String[] args) {

  77. System.out.println("-------------------歡迎使用撲克牌!--------------------");

  78. System.out.println("規(guī)則一:雙方發(fā)等量的牌,比較其中最大的牌,牌的點數(shù)是主要因素。");

  79. System.out.println("規(guī)則二:若點數(shù)相等,則繼續(xù)比較牌的花色,花色權重為紅桃>黑桃>方塊>梅花。");

  80. test t=new test();

  81. t.creat();

  82. }


  83. }


正在回答

3 回答

pokers[(i*13+j)]=new?poker(types[i],names[j]);

求解為什么是i*13+j

?

1 回復 有任何疑惑可以回復我~
#1

Linser 提問者

i是外層循環(huán),代表四種花色;j是內層循環(huán),代表13張不同點數(shù)的牌。代表全部的牌
2019-09-02 回復 有任何疑惑可以回復我~

大佬求膜拜

1 回復 有任何疑惑可以回復我~

https://img1.sycdn.imooc.com//5d36d217000116c405610401.jpg大致做出來了

1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

有些方法還是不太會,要參照老師講的用法才能寫對~

我要回答 關注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號