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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

再次實(shí)現(xiàn)一把,一年后再來,看看是否有所長(zhǎng)進(jìn)....

  • 演示效果:

    http://img1.sycdn.imooc.com//5f9677d80001ce6410680822.jpg

  • Card

/**
?*?撲克牌類
?*/
public?class?Card?implements?Comparable<Card>?{

????private?int?pointsScore;??//手牌的點(diǎn)數(shù)
????private?int?colourScore;??//花色的點(diǎn)數(shù)
????private?String?points;?//牌的名稱
????private?String?color;?//牌的花色

????public?Card(int?pointsScore,?int?colourScore,?String?points,?String?color)?{
????????this.pointsScore?=?pointsScore;
????????this.colourScore?=?colourScore;
????????this.points?=?points;
????????this.color?=?color;
????}

????//重寫Comparable類中compareTo()方法
????@Override
????public?int?compareTo(Card?c)?{
????????//如果點(diǎn)數(shù)一樣,則比較花色
????????if?(this.pointsScore?==?c.pointsScore)?{
????????????//這樣定是降序排序,反著寫就是升序排序
????????????return?c.colourScore?-?this.colourScore;
????????}
????????//比較花色,降序排序,也就是說容器中最大的下標(biāo)為0
????????return?c.pointsScore?-?this.pointsScore;
????}

????@Override
????public?String?toString()?{
????????return?this.color?+?this.points;
????}
????
}
  • Poker

import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;

/**
?*?整副撲克牌
?*/
public?class?Poker?{
????private?List<Card>?cards?=?new?ArrayList<>();

????public?Poker()?{
????????createPoker();
????????randomPoker();
????}

????/**
?????*?創(chuàng)建撲克牌
?????*/
????private?void?createPoker()?{
????????System.out.println("-------------創(chuàng)建撲克牌...--------------");
????????String[]?names?=?{"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",?"J",?"Q",?"K",?"A"};?//定義一個(gè)字符串?dāng)?shù)組
????????String[]?colors?=?{"方片",?"梅花",?"紅桃",?"黑桃"};

????????for?(int?i?=?0;?i?<?names.length;?i++)?{
????????????for?(int?j?=?0;?j?<?colors.length;?j++)?{
????????????????cards.add(new?Card(i,?j,?names[i],?colors[j]));
????????????}
????????}

????????System.out.println("-------------撲克牌創(chuàng)建完畢!--------------");
????????System.out.println(cards);?//輸出
????}

????/**
?????*?洗牌
?????*/
????public?void?randomPoker()?{
????????System.out.println("-------------開始洗牌--------------");
????????Collections.shuffle(cards);??//調(diào)用Collections類中shuffle()方法就好了
????????System.out.println("-------------洗牌結(jié)束!--------------");
????}

????/**
?????*?根據(jù)索引獲取撲克牌
?????*?@param?index
?????*?@return
?????*/
????public?Card?get(int?index)?{
????????return?this.cards.get(index);
????}

????/**
?????*?獲取撲克牌的數(shù)量
?????*?@return
?????*/
????public?int?size()?{
????????return?this.cards.size();
????}

}
  • Player

import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;

/**
?*?玩家類
?*/
public?class?Player?implements?Comparable<Player>?{

????private?int?id;?//玩家ID
????private?String?name;?//玩家姓名
????private?Card?maxCard;?//?玩家手中最大的牌
????private?List<Card>?cards?=?new?ArrayList<>();?//存放玩家手中的牌的容器

????public?Player(int?id,?String?name)?{
????????this.id?=?id;
????????this.name?=?name;
????}

????public?int?getId()?{
????????return?id;
????}

????public?String?getName()?{
????????return?name;
????}

????public?List<Card>?getCards()?{
????????return?cards;
????}

????//獲取玩家手中最大的牌的方法
????public?Card?getMaxCard()?{
????????if?(this.maxCard?==?null)?{
????????????//先排序,由于重寫了Poker類中compareTo()方法,下標(biāo)為0的為最大值
????????????Collections.sort(cards);
????????????this.maxCard?=?cards.get(0);
????????}
????????return?this.maxCard;
????}

????@Override
????public?int?compareTo(Player?o)?{
????????//?按照玩家最大的牌進(jìn)行排序,獲取玩家排在最前面
????????return?this.getMaxCard().compareTo(o.getMaxCard());
????}

????public?void?add(Card?card)?{
????????System.out.println("--玩家["?+?this.getName()?+?"]拿牌:"?+?card);
????????this.cards.add(card);
????}

????/**
?????*?輸出玩家最大的手牌
?????*/
????public?void?showMaxCard()?{
????????System.out.println("玩家["?+?this.getName()?+?"]最大的手牌為:"?+?this.getMaxCard());
????}

????/**
?????*?輸出玩家各自手牌
?????*/
????public?void?showCards()?{
????????System.out.println(this.getName()?+?":"?+?this.getCards());
????}

}
  • PokerGame

import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Scanner;
import?java.util.function.Function;

/**
?*?撲克游戲
?*/
public?class?PokerGame?{

????private?List<Player>?players?=?new?ArrayList<>();?//用來存放玩家
????private?Poker?poker?=?new?Poker();?//?整副撲克牌
????private?Scanner?scanner?=?new?Scanner(System.in);?//?接收用戶在控制臺(tái)輸入的信息

????/**
?????*?啟動(dòng)方法
?????*/
????public?void?startGame()?{
????????creatPlayer();?//創(chuàng)建玩家
????????dealPoker();?//?發(fā)牌
????????comparePoker();?//統(tǒng)計(jì)
????}

????/**
?????*?創(chuàng)建玩家
?????*/
????public?void?creatPlayer()?{
????????System.out.println("-------------創(chuàng)建玩家...--------------");

????????int?playerNum?=?getInput("請(qǐng)輸入玩家數(shù)量:",?playerCount?->?{
????????????if?(playerCount?<?2)?{
????????????????System.out.println("玩家數(shù)量最少有2位!請(qǐng)重新輸入!");
????????????????return?false;
????????????}
????????????if?(playerCount?>?poker.size())?{
????????????????System.out.println("輸入的玩家數(shù)量不能大于撲克牌的數(shù)量("?+?poker.size()?+?")!請(qǐng)重新輸入!");
????????????????return?false;
????????????}
????????????return?true;
????????});

????????for?(int?i?=?1;?i?<=?playerNum;?i++)?{?//這所以寫個(gè)循環(huán),是為了方便修改玩家的人數(shù),只需改一下循環(huán)的判斷條件就好了
????????????System.out.print("請(qǐng)輸入第"?+?i?+?"位玩家的姓名:");
????????????players.add(new?Player(i,?scanner.next()));
????????}

????????//?輸出各位玩家信息
????????for?(Player?player?:?players)?{
????????????System.out.println("----歡迎玩家:"?+?player.getName());
????????}
????}

????/**
?????*?發(fā)牌
?????*/
????public?void?dealPoker()?{
????????System.out.println("-------------開始發(fā)牌...--------------");

????????int?cardNum?=?getInput("請(qǐng)?jiān)O(shè)置給每個(gè)玩家發(fā)多少?gòu)埮疲?,?cardCount?->?{
????????????if?(cardCount?<=?0)?{
????????????????System.out.println("發(fā)牌數(shù)不能必須大于0,請(qǐng)重新輸入!");
????????????????return?false;
????????????}

????????????if?(cardCount?*?players.size()?>?poker.size())?{
????????????????System.out.println("非法的發(fā)牌數(shù),總發(fā)牌數(shù)量不能超過撲克牌的數(shù)量("?+?poker.size()?+?")!請(qǐng)重新輸入!");
????????????????return?false;
????????????}
????????????return?true;
????????});

????????int?limit?=?players.size()?*?cardNum;
????????for?(int?i?=?0;?i?<?limit;?i++)?{
????????????players.get(i?%?players.size()).add(poker.get(i));
????????}

????????System.out.println("-------------發(fā)牌結(jié)束...--------------");
????}

????/**
?????*?統(tǒng)計(jì)
?????*/
????public?void?comparePoker()?{
????????System.out.println("-------------開始統(tǒng)計(jì)...--------------");

????????players.forEach(Player::showMaxCard);

????????Player?winner?=?getWinner();
????????System.out.println("-------------玩家["?+?winner.getName()?+?"]獲勝!--------------");

????????System.out.println("玩家各自的手牌為:");
????????players.forEach(Player::showCards);
????}

????private?Player?getWinner()?{
????????//?對(duì)所有玩家進(jìn)行排序,排序規(guī)則參考Player.compareTo()方法
????????Collections.sort(players);
????????//?得到獲勝玩家
????????return?players.get(0);
????}

????/**
?????*?從控制臺(tái)接受用戶輸入的一個(gè)正整數(shù)
?????*
?????*?@param?hint?????提示信息
?????*?@param?function?用戶判斷用戶輸入的信息是否合法,如果不合法,需要重新輸入
?????*?@return
?????*/
????private?int?getInput(String?hint,?Function<Integer,?Boolean>?function)?{
????????while?(true)?{
????????????System.out.print(hint);
????????????try?{
????????????????int?input?=?scanner.nextInt();
????????????????if?(!function.apply(input))?{
????????????????????continue;
????????????????}
????????????????return?input;
????????????}?catch?(Exception?e)?{
????????????????System.out.println("輸入不合法!請(qǐng)重新輸入!");
????????????????//?如果出現(xiàn)異常,則跳過本次輸入的信息
????????????????scanner?=?scanner.skip(".*");
????????????}
????????}
????}
}
  • Test

/**
?*?測(cè)試類
?*/
public?class?Test?{
????public?static?void?main(String[]?args)?{
????????new?PokerGame().startGame();
????}
}


正在回答

1 回答

前輩,寫得好精簡(jiǎn)??!真厲害,感謝分享,我寫得好爛啊,不知道你之前是怎么提升的呢?

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

再次實(shí)現(xiàn)一把,一年后再來,看看是否有所長(zhǎng)進(jìn)....

我要回答 關(guān)注問題
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)