├── README.md ├── ch1-1 ├── HelloWorld.class ├── HelloWorld.java └── Test.java ├── ch4-1 ├── MulTable.class └── MulTable.java ├── ch4-2 ├── Trangle.class └── Trangle.java ├── ch4-3 ├── Circle.class └── Circle.java ├── ch4-4 ├── SubString.class └── SubString.java ├── ch4-5 ├── Rmb.class └── Rmb.java ├── ch4-6 ├── ChessBoard.class ├── ChessBoard.java ├── ChessMan.class ├── ChessMan.java ├── FiveChessGame.class └── FiveChessGame.java ├── ch5-1 ├── Student.class └── Student.java ├── ch6-1 ├── Car.class ├── Car.java ├── GeneralGroundVehicle.class ├── GeneralGroundVehicle.java ├── Tractor.class └── Truck.class ├── ch6-2 ├── Movable$1.class ├── Movable.class └── Movable.java ├── ch6-3 ├── Movable.class └── Movable.java ├── ch6-4 ├── Card.class ├── FiveCardStud.class ├── FiveCardStud.java └── Player.class ├── ch7-1 ├── Calc.class └── Calc.java ├── ch7-2 ├── StringCut.class └── StringCut.java ├── ch7-3 ├── ScannerABC.class └── ScannerABC.java ├── ch7-4 ├── ChessBoard.class ├── ChessBoard.java ├── ChessMan.class ├── ChessMan.java ├── FiveChessGame.class ├── FiveChessGame.java ├── FiveChessGame_zh.properties └── FiveChessGame_zh_CN.properties ├── ch8-1 ├── CreateSet.class └── CreateSet.java ├── ch8-2 ├── PracticeList.class └── PracticeList.java ├── ch8-3 ├── PracticeMap.class └── PracticeMap.java └── ch8-4 ├── FiveCardStud$Card.class ├── FiveCardStud$Player.class ├── FiveCardStud.class └── FiveCardStud.java /README.md: -------------------------------------------------------------------------------- 1 | # CrazyJavaExcercisesSolutions 2 | 3 | 有些解答是有BUG的,其中有些是我没发现,有些是懒得改了。 4 | 5 | 根据我做题的进度更新。希望我能做得快一点。 6 | 7 | 上传了一些class文件,但这只是因为我懒得一个个上传了。不保证class文件的正确性,请读者自己编译再运行。 8 | 9 | # 编码问题 10 | 第一步: 11 | 12 | 源文件大多使用utf-8,但是也有部分gbk编码的。 13 | 建议将gbk编码的文件转为utf-8后再运行。 14 | 15 | 但是这样依然不能运行,因为中国Windows默认编码是GBK。 16 | 17 | 所以需要第二步,有两种方案: 18 | ## 解决方案1 19 | 在每个javac命令的末尾加上-Dfile.encoding=UTF-8;例如 20 | ``` 21 | javac demo.java -Dfile.encoding=UTF-8 22 | ``` 23 | ## 解决方案2 24 | 设置环境变量JAVA_TOOL_OPTIONS 的值为 -Dfile.encoding=UTF-8 25 | 26 | 然后重启命令行。 27 | -------------------------------------------------------------------------------- /ch1-1/HelloWorld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch1-1/HelloWorld.class -------------------------------------------------------------------------------- /ch1-1/HelloWorld.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch1-1/HelloWorld.java -------------------------------------------------------------------------------- /ch1-1/Test.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch1-1/Test.java -------------------------------------------------------------------------------- /ch4-1/MulTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch4-1/MulTable.class -------------------------------------------------------------------------------- /ch4-1/MulTable.java: -------------------------------------------------------------------------------- 1 | public class MulTable{ 2 | public static void main(String[] args){ 3 | for(var i=1;i<=9;i++){ 4 | for(var j=1;j<=i;j++){ 5 | if(j>1)System.out.print(","); 6 | System.out.print(i+"*"+j+"="+i*j); 7 | } 8 | System.out.print("\n"); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /ch4-2/Trangle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch4-2/Trangle.class -------------------------------------------------------------------------------- /ch4-2/Trangle.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.io.IOException; 4 | import java.util.Arrays; 5 | public class Trangle{ 6 | public static void main(String[] args){ 7 | BufferedReader br = new BufferedReader( 8 | new InputStreamReader(System.in) 9 | ); 10 | int n; 11 | try{ 12 | n = Integer.parseInt(br.readLine()); 13 | }catch(IOException e){ 14 | System.out.println("IOException."); 15 | //e.printStack(); 16 | return; 17 | } 18 | 19 | char[] str = new char[2*n-1]; 20 | for(int i=1;i<=n;i++){ 21 | Arrays.fill(str,' '); 22 | for(int j=n-i;j1; 30 | } 31 | private static String subStringByByte(String s,int begin,int end)throws UnsupportedEncodingException{ 32 | int sbegin = 0;//begin index of s. 33 | int bytesnum = 0;//num of bytes that is already counted. 34 | for(;bytesnumend){ 51 | idx--; 52 | } 53 | return s.substring(sbegin,idx); 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /ch4-5/Rmb.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch4-5/Rmb.class -------------------------------------------------------------------------------- /ch4-5/Rmb.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch4-5/Rmb.java -------------------------------------------------------------------------------- /ch4-6/ChessBoard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch4-6/ChessBoard.class -------------------------------------------------------------------------------- /ch4-6/ChessBoard.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class ChessBoard{ 3 | public static final int BOARDSIZE = 22; 4 | private String[][] board; 5 | public ChessBoard(){ 6 | board = new String[BOARDSIZE][BOARDSIZE]; 7 | } 8 | public static boolean isWithinRange(int x,int y){ 9 | return 1<=x && x<=BOARDSIZE && 1<=y && y<=BOARDSIZE; 10 | } 11 | public void init(){ 12 | for(int i=0;i=WIN_COUNT || 100 | up+down+1>=WIN_COUNT || 101 | leftDown+rightUp+1>=WIN_COUNT || 102 | leftUp+rightDown+1>=WIN_COUNT ){ 103 | if(chessMark.equals(ChessMan.BLACK.getChessMark())){ 104 | System.out.println("You win!"); 105 | }else{ 106 | System.out.println("You lose."); 107 | } 108 | return true; 109 | } 110 | return false; 111 | } 112 | public int start()throws Exception{ 113 | while(true){ 114 | run(); 115 | if(!isReplay())break; 116 | } 117 | return 0; 118 | } 119 | private boolean isReplay()throws IOException{ 120 | System.out.println("Replay?(y/n)"); 121 | if(br.readLine().strip().equals("y"))return true; 122 | return false; 123 | } 124 | private boolean isValid(String inputStr){ 125 | String[] inputStrs = inputStr.strip().split(","); 126 | try{ 127 | posX = Integer.parseInt(inputStrs[0]); 128 | posY = Integer.parseInt(inputStrs[1]); 129 | }catch(Exception e){ 130 | System.out.println("Fail:Input form not Valid."); 131 | } 132 | if(posX<1||posX>ChessBoard.BOARDSIZE || 133 | posY<1||posY>ChessBoard.BOARDSIZE 134 | ){ 135 | System.out.println("Fail:The range of coordinates must be between 1 and "+ChessBoard.BOARDSIZE+"."); 136 | return false; 137 | } 138 | if(!chessBoard.getPos(posX,posY).equals("ʮ")){ 139 | System.out.println("Fail:That location is already occupied."); 140 | return false; 141 | } 142 | return true; 143 | } 144 | } -------------------------------------------------------------------------------- /ch5-1/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch5-1/Student.class -------------------------------------------------------------------------------- /ch5-1/Student.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch5-1/Student.java -------------------------------------------------------------------------------- /ch6-1/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-1/Car.class -------------------------------------------------------------------------------- /ch6-1/Car.java: -------------------------------------------------------------------------------- 1 | class Car extends GeneralGroundVehicle{ 2 | public void move(){ 3 | System.out.println("Car Moving..."); 4 | } 5 | public Car(int personsCapacity){ 6 | super(personsCapacity); 7 | } 8 | } 9 | class Truck extends GeneralGroundVehicle{ 10 | public void move(){ 11 | System.out.println("Truck Moving..."); 12 | } 13 | public Truck(int personsCapacity){ 14 | super(personsCapacity); 15 | } 16 | } 17 | class Tractor extends GeneralGroundVehicle{ 18 | public void move(){ 19 | System.out.println("Tractor Moving..."); 20 | } 21 | public Tractor(int personsCapacity){ 22 | super(personsCapacity); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ch6-1/GeneralGroundVehicle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-1/GeneralGroundVehicle.class -------------------------------------------------------------------------------- /ch6-1/GeneralGroundVehicle.java: -------------------------------------------------------------------------------- 1 | public abstract class GeneralGroundVehicle{ 2 | private int currentPersonsNum; 3 | private int personsCapacity; 4 | public abstract void move(); 5 | public GeneralGroundVehicle(){ 6 | currentPersonsNum=0; 7 | personsCapacity=0; 8 | } 9 | public GeneralGroundVehicle(int personsCapacity){ 10 | this.personsCapacity=personsCapacity; 11 | } 12 | public int getPersonsCapacity(){return personsCapacity;}; 13 | public int getCurrentPersonsNum(){ 14 | return currentPersonsNum; 15 | } 16 | public boolean loadPerson(){ 17 | if(currentPersonsNumSystem.out.println("I am moving..."); 9 | if(mv.isMovable())mv.move(); 10 | } 11 | } -------------------------------------------------------------------------------- /ch6-4/Card.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-4/Card.class -------------------------------------------------------------------------------- /ch6-4/FiveCardStud.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-4/FiveCardStud.class -------------------------------------------------------------------------------- /ch6-4/FiveCardStud.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-4/FiveCardStud.java -------------------------------------------------------------------------------- /ch6-4/Player.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch6-4/Player.class -------------------------------------------------------------------------------- /ch7-1/Calc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch7-1/Calc.class -------------------------------------------------------------------------------- /ch7-1/Calc.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Calc{ 3 | 4 | public static void main(String[] args){ 5 | int[] a = new int[10]; 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Please input 10 Integer:"); 8 | double average=0,maxValue=0,minValue=0; 9 | for(int i=0;i<10;i++){ 10 | a[i]=sc.nextInt(); 11 | average+=a[i]; 12 | if(i==0){ 13 | maxValue=a[i]; 14 | minValue=a[i]; 15 | }else{ 16 | maxValue = Math.max(maxValue,a[i]); 17 | minValue = Math.min(minValue,a[i]); 18 | } 19 | } 20 | average/=10; 21 | System.out.println("average="+average+",maxValue="+maxValue+",minValue="+minValue); 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /ch7-2/StringCut.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch7-2/StringCut.class -------------------------------------------------------------------------------- /ch7-2/StringCut.java: -------------------------------------------------------------------------------- 1 | public class StringCut{ 2 | public static void main(String[] args){ 3 | StringBuilder sb = new StringBuilder("ABCDEFG"); 4 | System.out.println(sb); 5 | String toDeleteStr = "CD"; 6 | int toDeleteIndex = sb.indexOf(toDeleteStr); 7 | sb.delete(toDeleteIndex,toDeleteIndex+toDeleteStr.length()); 8 | System.out.println(sb); 9 | toDeleteStr = "B"; 10 | toDeleteIndex = sb.indexOf(toDeleteStr); 11 | sb.delete(toDeleteIndex,toDeleteIndex+toDeleteStr.length()); 12 | System.out.println(sb); 13 | toDeleteStr = "F"; 14 | toDeleteIndex = sb.indexOf(toDeleteStr); 15 | sb.delete(toDeleteIndex,toDeleteIndex+toDeleteStr.length()); 16 | System.out.println(sb); 17 | } 18 | } -------------------------------------------------------------------------------- /ch7-3/ScannerABC.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch7-3/ScannerABC.class -------------------------------------------------------------------------------- /ch7-3/ScannerABC.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.regex.*; 3 | import java.util.Arrays; 4 | 5 | public class ScannerABC{ 6 | public static void main(String[] args){ 7 | String inputStr = "A1B2C3D4E5F6G7H8"; 8 | int[] numberArray = new int[8]; 9 | String[] strArray = new String[8]; 10 | Pattern wPattern = Pattern.compile("[a-zA-Z]+"); 11 | Pattern dPattern = Pattern.compile("\\d+"); 12 | Matcher wMatcher = wPattern.matcher(inputStr); 13 | Matcher dMatcher = dPattern.matcher(inputStr); 14 | for(int i=0;i<8;i++){ 15 | if(wMatcher.find())strArray[i]=wMatcher.group(); 16 | if(dMatcher.find())numberArray[i]=Integer.parseInt(dMatcher.group()); 17 | } 18 | System.out.println(Arrays.toString(strArray)); 19 | System.out.println(Arrays.toString(numberArray)); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /ch7-4/ChessBoard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch7-4/ChessBoard.class -------------------------------------------------------------------------------- /ch7-4/ChessBoard.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class ChessBoard{ 3 | public static final String EMPTY_MARK = "十"; 4 | public static final int BOARDSIZE = 22; 5 | private String[][] board; 6 | public ChessBoard(){ 7 | board = new String[BOARDSIZE][BOARDSIZE]; 8 | } 9 | public static boolean isWithinRange(int x,int y){ 10 | return 1<=x && x<=BOARDSIZE && 1<=y && y<=BOARDSIZE; 11 | } 12 | public void init(){ 13 | for(int i=0;i=WIN_COUNT || 133 | up+down+1>=WIN_COUNT || 134 | leftDown+rightUp+1>=WIN_COUNT || 135 | leftUp+rightDown+1>=WIN_COUNT ){ 136 | 137 | return true; 138 | } 139 | return false; 140 | } 141 | /** 142 | 开始游戏。 143 | */ 144 | public int start()throws Exception{ 145 | while(true){ 146 | run(); 147 | if(!isReplay())break; 148 | } 149 | return 0; 150 | } 151 | /** 152 | 是否再来一局。 153 | */ 154 | private boolean isReplay()throws IOException{ 155 | System.out.println(bundle.getString("AskReplay")); 156 | if(br.readLine().strip().equals("y"))return true; 157 | return false; 158 | } 159 | /** 160 | 判断输入是否合法。 161 | 根据题目要求进行了修改,使用了正则表达式。 162 | */ 163 | private boolean isValid(String inputStr){ 164 | String inputStrs = inputStr.strip(); 165 | Matcher matcher = Pattern.compile("^(\\d+),(\\d+)$").matcher(inputStr); 166 | try{ 167 | if(matcher.find()){ 168 | posX = Integer.parseInt(matcher.group(1)); 169 | posY = Integer.parseInt(matcher.group(2)); 170 | }else{ 171 | throw new Exception(); 172 | } 173 | }catch(Exception e){ 174 | //System.out.println("Fail:Input form not Valid."); 175 | return false; 176 | } 177 | if(posX<1||posX>ChessBoard.BOARDSIZE || 178 | posY<1||posY>ChessBoard.BOARDSIZE 179 | ){ 180 | System.out.println( 181 | MessageFormat.format( 182 | bundle.getString("InputOutOfRangeHint"),ChessBoard.BOARDSIZE 183 | ) 184 | ); 185 | return false; 186 | } 187 | if(!chessBoard.getPos(posX,posY).equals(ChessBoard.EMPTY_MARK)){ 188 | System.out.println(bundle.getString("InputPositionOccupiedHint")); 189 | return false; 190 | } 191 | return true; 192 | } 193 | } -------------------------------------------------------------------------------- /ch7-4/FiveChessGame_zh.properties: -------------------------------------------------------------------------------- 1 | InputHint=请输入您要下棋的位置,格式为 x,y : 2 | InvalidInputHint=输入格式不正确。 3 | AskReplay=再玩一局?(y/n) 4 | InputOutOfRangeHint=错误:坐标范围必须在1到{0}之间. 5 | InputPositionOccupiedHint=错误:该位置已经有棋子. 6 | YouWin=你赢了! 7 | YouLose=你输了! -------------------------------------------------------------------------------- /ch7-4/FiveChessGame_zh_CN.properties: -------------------------------------------------------------------------------- 1 | InputHint=Input where you chess, form is x,y : 2 | InvalidInputHint=Input invalid. 3 | AskReplay=Replay?(y/n) 4 | InputOutOfRangeHint=Fail:The range of coordinates must be between 1 and {0}. 5 | InputPositionOccupiedHint=Fail:That location is already occupied. 6 | YouWin=You win! 7 | YouLose=You lose! -------------------------------------------------------------------------------- /ch8-1/CreateSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-1/CreateSet.class -------------------------------------------------------------------------------- /ch8-1/CreateSet.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.HashSet; 3 | /** 4 | @author linyu003 5 | */ 6 | public class CreateSet{ 7 | public static void main(String[] args){ 8 | // 这里如果不使用,会产生Xlint警告,尽管不影响程序运行。 9 | // 根据书中提到的原则,我们应该在集合中只存储同一种类型的对象, 10 | // 因此这个类型是可以确定的。 11 | var set = new HashSet(); 12 | var sc = new Scanner(System.in).useDelimiter("\n"); 13 | //输入20个字符串太麻烦,因此改成了3个。 14 | for(int i=1;i<=3;i++){ 15 | set.add(sc.next()); 16 | } 17 | // 应当清楚,hashset遍历是乱序的,如果没有看到乱序输出, 18 | // 可能是因为个数过少导致的偶然,可以考虑增加输入个数。 19 | set.forEach(str->System.out.println(str)); 20 | } 21 | } -------------------------------------------------------------------------------- /ch8-2/PracticeList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-2/PracticeList.class -------------------------------------------------------------------------------- /ch8-2/PracticeList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class PracticeList{ 3 | public static void main(String[] args){ 4 | var books = new ArrayList(); 5 | books.add("book0"); 6 | books.add("book1"); 7 | books.add("book2"); 8 | books.add("book3"); 9 | books.add("book4"); 10 | books.add("book5"); 11 | books.add("book6"); 12 | books.add("book7"); 13 | books.add("book8"); 14 | books.add("book9"); 15 | System.out.println(books.get(5)); 16 | System.out.println(books.indexOf(new String("book1"))); 17 | System.out.println(books.indexOf(new String("book9"))); 18 | books.remove(3); 19 | books.forEach(book->System.out.println(book)); 20 | 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /ch8-3/PracticeMap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-3/PracticeMap.class -------------------------------------------------------------------------------- /ch8-3/PracticeMap.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | public class PracticeMap{ 3 | public static void main(String[] args){ 4 | String[] strs = {"a","b","a","b","c","a","b","c","b"}; 5 | var map = new HashMap(); 6 | for(String str:strs){ 7 | if(!map.containsKey(str))map.put(str,1); 8 | else map.replace(str,(Integer)map.get(str)+1); 9 | } 10 | map.forEach((key,value)->System.out.println(key+":"+value)); 11 | } 12 | } -------------------------------------------------------------------------------- /ch8-4/FiveCardStud$Card.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-4/FiveCardStud$Card.class -------------------------------------------------------------------------------- /ch8-4/FiveCardStud$Player.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-4/FiveCardStud$Player.class -------------------------------------------------------------------------------- /ch8-4/FiveCardStud.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/linyu003/CrazyJavaExcercisesSolutions/085f86b77bd36cd2c83995ce5ee4db60cd58f7bc/ch8-4/FiveCardStud.class -------------------------------------------------------------------------------- /ch8-4/FiveCardStud.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.ArrayList; 3 | /** 4 | 相对于第六章的游戏,没有增加更多的功能,仅仅运用Collections进行了一些改写(cards 和 players 的存储)。 5 | @author linyu003 6 | */ 7 | public class FiveCardStud{ 8 | public static final int PLAYER_NUM = 5; 9 | public static final String[] SHAPE = {"方片","红桃","黑桃","梅花"}; 10 | public static final String[] VALUE = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; 11 | public static final int SHAPE_NUM = SHAPE.length; 12 | public static final int VALUE_NUM = VALUE.length; 13 | public static final int CARD_NUM = SHAPE_NUM*VALUE_NUM; 14 | private ArrayList players = new ArrayList<>(); 15 | private LinkedList cards = new LinkedList<>(); 16 | int dealNum; 17 | public boolean quit(int seatNumber){ 18 | return players.get(seatNumber).quit(); 19 | } 20 | public FiveCardStud(){ 21 | dealNum = 0; 22 | for(int i=0;iplayer.printStatus()); 38 | } 39 | public void dealCards(){ 40 | dealNum++; 41 | players.forEach(player->{ 42 | if(!player.isQuitCheck()){ 43 | player.addCard(cards.get(0)); 44 | cards.remove(0); 45 | } 46 | }); 47 | } 48 | public boolean isEnd(){return dealNum>=5;} 49 | 50 | 51 | 52 | private static class Player{ 53 | private final int seatNumber; 54 | boolean isQuit = false; 55 | private int cardNum = 0; 56 | public static final int MAX_CARD_NUM = 5; 57 | private LinkedList cards = new LinkedList<>(); 58 | 59 | Player(int seatNumber){ 60 | this.seatNumber = seatNumber; 61 | } 62 | boolean isQuitCheck(){return isQuit;} 63 | void printStatus(){ 64 | System.out.print("Player#"+seatNumber+": "); 65 | if(isQuitCheck())System.out.print("已放弃比赛。"); 66 | else{ 67 | System.out.print("手牌"+cardNum+"张,"); 68 | cards.forEach(card->System.out.print(card.getShape()+card.getValue()+" ")); 69 | } 70 | System.out.print("\n"); 71 | } 72 | boolean addCard(Card card){ 73 | if(cardNum>=MAX_CARD_NUM){ 74 | System.out.println("Player#"+seatNumber+"can't add more cards."); 75 | return false; 76 | } 77 | cards.add(card); 78 | return true; 79 | } 80 | boolean quit(){ 81 | if(isQuitCheck()){ 82 | System.out.println("Player#"+seatNumber+" is already quited, can't quit again."); 83 | return false; 84 | } 85 | isQuit=true; 86 | System.out.println("Player#"+seatNumber+" quits."); 87 | return true; 88 | } 89 | } 90 | private static class Card{ 91 | private final String value; 92 | private final String shape; 93 | Card(String shape,String value){ 94 | this.value = value; 95 | this.shape = shape; 96 | } 97 | String getValue(){return this.value;} 98 | String getShape(){return this.shape;} 99 | } 100 | 101 | public static void main(String[] args){ 102 | FiveCardStud game = new FiveCardStud(); 103 | while(!game.isEnd()){ 104 | game.dealCards(); 105 | game.quit(1); 106 | game.printStatus(); 107 | } 108 | } 109 | } 110 | --------------------------------------------------------------------------------