-
public void 方法名(参数列表)throws 异常列表{ //调用会抛出异常的方法或者: throw new Exception(); }
查看全部 -
在多重Catch块后面,可以加个catch(Exception)
查看全部 -
finally语句块有啥用?
善后工作:如关闭连接,关闭一些有档案的文件
查看全部 -
为什么要先子类后父类的顺序编写多重catch语句块呢??
因为当程序抛出异常时, 异常处理系统会就近匹配异常的处理程序
查看全部 -
首先:抛出异常的方法会终止执行
随后:程序的控制权会被移交给catch块中的异常处理程序
查看全部 -
try 语句抛出可能出现的异常
catch捕获出现的异常,有处理异常的相应代码
查看全部 -
异常Exception非检查异常
查看全部 -
throw——将产生的异常抛出(动作){指将异常抛出的这个动作,需要throws先声明!)
throws——声明将要抛出何种类型的异常(声明)
public void 方法名(参数列表)
throws异常列表{
//调用会抛出异常的方法或者:
throw new Exception();
}
查看全部 -
1、处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
2、在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
3、对于不确定的代码,也可以加上try-catch,处理潜在的异常
4、尽量去处理异常,切忌只是简单的调用printStackTrace()去打印输出异常
5、具体如何处理异常,要根据不同的也无需求和异常类型去决定
6、尽量添加finally语句块去释放占用的资源
查看全部 -
foreach循环:
for(元素类型t 元素变量x : 遍历对象obj){ }
查看全部 -
Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: Math.round();
for循环遍历数组的简单写法 for(int num :nums)
这里的int num 表示nums里元素的类型int num用来统称表示其中元素
查看全部 -
注意for循环2种循环体位置不同所带来的区别(此题目中会导致数组越界)问题出现的原因是判断先后的问题方法1 中先判断后执行 方法2中i>0 但是i-3的值没法判断进入循环体后i-3实际情况小于0导致数组越界!
for(int i = str.length(); i>0; i=i-3){
str.insert (i,',');
}
for(int i = str.length(); i>3;){
i=i-3;
str.insert (i,',');
}
查看全部 -
深入理解StringBulider StringBuffer String的区别
查看全部 -
Cards.java
PokerCards.java
Player.java
PokerPlayer.java
Game.java (住程序入口)
Cards.java
package com.imooc; import java.util.HashMap; import java.util.Map; public class Cards implements Comparable<Cards>{ /** * 卡牌属性:点色,大小 */ private String flower; private String number; public Cards() { } public Cards(String flowString, String numString) { this.flower = flowString; this.number = numString; } public String getFlower() { return flower; } public void setFlower(String flower) { this.flower = flower; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } /** * 实现卡牌可以比较大小(和排序?) */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((flower == null) ? 0 : flower.hashCode()); result = prime * result + ((number == null) ? 0 : number.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Cards)) return false; Cards other = (Cards) obj; if (flower == null) { if (other.flower != null) return false; } else if (!flower.equals(other.flower)) return false; if (number == null) { if (other.number != null) return false; } else if (!number.equals(other.number)) return false; return true; } /** * > 比较点数 -> 胜负 * > 若点数一样,比较花色 * 黑 > 红 > 梅 > 方 */ @Override public int compareTo(Cards o) { // TODO Auto-generated method stub final String[] pokerPoints = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; int thisNumberIndex = 0; int oNumberIndex = 0; for(int i = 0; i < pokerPoints.length; i++) { if(this.getNumber().equals(pokerPoints[i])) { thisNumberIndex = i; } } for (int i = 0; i < pokerPoints.length; i++) { if(o.getNumber().equals(pokerPoints[i])) { oNumberIndex = i; } } // 点数下标越小则牌面越小 if(thisNumberIndex < oNumberIndex) { return -1; } else if (thisNumberIndex > oNumberIndex) { return 1; } else { // 点数相等的情况下比较花色 String[] flowStrings = {"黑桃","红桃","梅花","方块"}; int thisIndex = 0; int oIndex = 0; // 获取当前卡牌花色的优先级(数组下标) for(int i = 0; i < flowStrings.length; i++) { if(this.getFlower() == flowStrings[i]) { thisIndex = i; break; } } // 获取传入卡牌花色的优先级 for (int i = 0; i < flowStrings.length; i++) { if(o.getFlower() == flowStrings[i]) { oIndex = i; break; } } //比较花色大小,数组下标越小则卡牌越大 if(thisIndex < oIndex) { return 1; } else if (thisIndex > oIndex) { return -1; } else if (thisIndex == oIndex) { return 0; } } return 0; } }
PokerCards.java
package com.imooc; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class PokerCards { /** * 扑克牌 * - 四种花色:黑桃,红桃,梅花,方块 * - 十三种点数: 2-10,J,Q,K,A (不考虑大小王) * - 共计52张 */ private List<Cards> poker; public PokerCards() { poker = new ArrayList<Cards>(); final String[] flower = {"黑桃","红桃","梅花","方块"}; final String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; System.out.println("----------创建卡牌----------"); for(int i = 0; i < flower.length; i++) { for(int j = 0; j< points.length; j++) { poker.add(new Cards(flower[i],points[j])); } } for (Cards card : poker) { System.out.print("卡牌: " + card.getFlower() + card.getNumber() + " "); } } public void shufflePoker() { System.out.println("----------洗牌开始----------"); Collections.shuffle(poker); System.out.println("----------洗牌结束----------"); for (Cards card : poker) { System.out.print("卡牌: " + card.getFlower() + card.getNumber() + " "); } System.out.println(); } public Cards dealCards(int cardSeq) { return poker.get(cardSeq); } public void compareCards(Cards cd) { } }
Player.java
package com.imooc; import java.util.ArrayList; import java.util.List; /** * 玩家 * 属性: ID,姓名,手牌 * @author CLAUCHEN * */ public class Player { private String ID; private String name; public List<Cards> hand; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Player(String ID, String name) { this.ID = ID; this.name = name; hand = new ArrayList<Cards>(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((ID == null) ? 0 : ID.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Player)) return false; Player other = (Player) obj; if (ID == null) { if (other.ID != null) return false; } else if (!ID.equals(other.ID)) return false; return true; } }
PokerPlayer.java
package com.imooc; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; /** *玩家 * 属性: ID,姓名,手牌(扑克牌的集合) * @author CLAUCHEN * */ public class PokerPlayer { public Map<String, Player> pokerPlayer; // 构造函数 public PokerPlayer() { // TODO Auto-generated constructor stub this.pokerPlayer = new HashMap<String, Player>(); } /** * 添加玩家,从控制台输入玩家ID和姓名 * 如果ID已经存在,则请用户重新输入 * 如果ID不存在,则添加该用户 */ public void playerAdd(int playerNum) { int count = 0; while(count < playerNum) { System.out.println("请输入第" + (count+1) + "位玩家ID: "); // 创建Scanner从控制台获取用户输入 Scanner console = new Scanner(System.in); String tempID = console.next(); if (! pokerPlayer.containsValue(new Player(tempID, null))) { // 用户输入的玩家不存在,将它添加到pokerPlayer中 // 继续获取玩家姓名 System.out.println("请输入第" + (count+1) + "位玩家姓名: "); String tempName = console.next(); pokerPlayer.put(("玩家"+count), new Player(tempID, tempName)); count++; } else { System.out.println("玩家ID已经存在!请重新输入"); continue; } } // 输出欢迎信息 Set<String> keys = pokerPlayer.keySet(); for (String key : keys) { Player player = pokerPlayer.get(key); if (player != null) { // System.out.println(key); System.out.println("-----欢迎玩家: " + player.getName()); } } } /** * 玩家拿牌 * */ public void addDeal(String ID, Cards card) { System.out.println("-----玩家" + pokerPlayer.get(ID).getName() + "-拿牌"); pokerPlayer.get(ID).hand.add(card); } /** * 查询玩家X的手牌 */ public void checkDeal() { Set<String> playerKeySet = pokerPlayer.keySet(); for (String playerKey : playerKeySet) { System.out.println("玩家" + pokerPlayer.get(playerKey).getName() + "的手牌是: "); for (Cards card : pokerPlayer.get(playerKey).hand) { System.out.print(" " + card.getFlower()+card.getNumber() + " "); } System.out.println(); } } /** * 查询玩家X手牌最大点数 */ public Cards maxPlayerCard(String ID) { Collections.sort(pokerPlayer.get(ID).hand); Cards maxCard = pokerPlayer.get(ID).hand.get(pokerPlayer.get(ID).hand.size()-1); System.out.println("玩家" + pokerPlayer.get(ID).getName() +"手上最大的牌是: " + maxCard.getFlower() + maxCard.getNumber()); return maxCard; } }
主程序main
package com.imooc; public class Game { /** * 扑克牌 * - 四种花色:黑桃,红桃,梅花,方块 * - 十三种点数: 2-10,J,Q,K,A (不考虑大小王) * - 共计52张 * 两名玩家 * 属性: ID,姓名,手牌(扑克牌的集合) * * 洗牌,将扑克牌顺序打乱 * 发牌.每名玩家两张 * 第一张 - Player one * 第二张 - Player two * 第三张 - Player one * 第四张 - Player two * 游戏(比大小) * > 先取各自玩家手牌中点数大的牌 * > 比较点数 -> 胜负 * > 若点数一样,比较花色 * 黑 > 红 > 梅 > 方 * 输出赢家 */ public static void main(String[] args) { // TODO Auto-generated method stub // Game game = new Game(); /* * 测试 */ // 创建扑克牌和洗牌 PokerCards pokerCards = new PokerCards(); pokerCards.shufflePoker(); // 创建两名玩家: 系统中存储:玩家0,玩家1 // 用户输入玩家0的ID, name // 用户输入玩家1的ID, name PokerPlayer pokerPlayer = new PokerPlayer(); pokerPlayer.playerAdd(2); // 开始发牌,每名玩家两张 System.out.println("----------开始发牌----------"); for (int i = 0; i < 4; i++) { if(i % 2 == 0) { Cards playerOnecard = pokerCards.dealCards(i); pokerPlayer.addDeal("玩家"+(i%2), playerOnecard); } else { Cards playerTwocard = pokerCards.dealCards(i); pokerPlayer.addDeal("玩家"+(i%2), playerTwocard); } } // 发牌结束 System.out.println("-----------发牌结束----------"); // 开始游戏 System.out.println("-----------开始游戏----------"); // 获取玩家手上最大的牌 Cards[] playerMaxCards = new Cards[pokerPlayer.pokerPlayer.size()]; for(int playerNum = 0; playerNum < pokerPlayer.pokerPlayer.size(); playerNum++) { playerMaxCards[playerNum] = pokerPlayer.maxPlayerCard("玩家"+playerNum); } // 输出获胜玩家 int winnerIndex = 0; Cards tempMax = playerMaxCards[0]; for (int i = 1; i < playerMaxCards.length; i++) { if (tempMax.compareTo(playerMaxCards[i]) < 0) { tempMax = playerMaxCards[i]; winnerIndex = i; } } System.out.println("----------玩家: " + pokerPlayer.pokerPlayer.get("玩家"+winnerIndex).getName() + "获胜!"); // 输出玩家各自的手牌 System.out.println("玩家各自的手牌是: "); pokerPlayer.checkDeal(); } }
查看全部 -
for each方法是iterate(迭代器遍历)得宜中简便用法
查看全部
举报