国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Head first java 第五章(二)

2019-11-06 07:26:53
字體:
來源:轉載
供稿:網友

DotCom類

import java.util.ArrayList;public class DotCom { PRivate ArrayList<String> locationCells; private String name; public void setLocationCells(ArrayList<String> loc){ locationCells = loc; //方法 } public void setName(String n){ name = n; //方法 } public String checkYourself(String userInput){ String result = "miss"; int index = locationCells.indexOf(userInput); if(index >= 0){ //if猜中 locationCells.remove(index); if(locationCells.isEmpty()){ result = "kill"; //若locationCells為空,則該DotCom被kill System.out.println(name+" go die"); }else{ result = "hit"; //若不為空,則result為hit } } return result; //checkYourself方法返回result }}

DotComBust類

import java.util.ArrayList;public class DotComBust { private GameHelper helper = new GameHelper(); private ArrayList<DotCom> dotComList = new ArrayList<DotCom>(); private int numOfGuesses = 0; private void setUpGame(){ DotCom one = new DotCom(); one.setName("dog.com"); DotCom two = new DotCom(); two.setName("ac.com"); DotCom three = new DotCom(); three.setName("bili.com"); dotComList.add(one); dotComList.add(two); dotComList.add(three); //初始化,命名及加入ArrayList System.out.println("kaishi"); for(DotCom dotComToSet : dotComList){ ArrayList<String> newLocation = helper.placeDotCom(3); dotComToSet.setLocationCells(newLocation); } } private void startPlaying(){ while(!dotComList.isEmpty()){ String userGuess = helper.getUserInput("enter a guess"); checkUserGuess(userGuess); //調用checkUserGuess方法檢查輸入 } finishGame(); } private void checkUserGuess(String userGuess){ numOfGuesses++; String result = "miss"; for(DotCom dotComToTest :dotComList){ result = dotComToTest.checkYourself(userGuess); //調用checkYourself方法 if (result.equals("hit")){ break; //checkYourself方法返回result為hit,則輸出結果 } if(result.equals("kill")){ dotComList.remove(dotComToTest); break; //checkYourself方法返回result為kill,從DotComList移除DotComToTest } } System.out.println(result); } private void finishGame(){ System.out.println("你贏了"); if(numOfGuesses <= 18){ System.out.println("一共"+numOfGuesses+"次完成GameHelper類(該類主要用處為設置DotCom位置)

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;public class GameHelper { private static final String alphabet = "abcdefg"; private int gridLength = 7; private int gridSize = 49; private int [] grid = new int[gridSize]; private int comCount = 0; public String getUserInput(String prompt) { String inputLine = null; System.out.print(prompt + " "); try { BufferedReader is = new BufferedReader( new InputStreamReader(System.in)); inputLine = is.readLine(); if (inputLine.length() == 0 ) return null; } catch (IOException e) { System.out.println("IOException: " + e); } return inputLine.toLowerCase(); } public ArrayList<String> placeDotCom(int comSize) { // line 19 ArrayList<String> alphaCells = new ArrayList<String>(); String [] alphacoords = new String [comSize]; // holds 'f6' type coords String temp = null; // temporary String for concat int [] coords = new int[comSize]; // current candidate coords int attempts = 0; // current attempts counter boolean success = false; // flag = found a good location ? int location = 0; // current starting location comCount++; // nth dot com to place int incr = 1; // set horizontal increment if ((comCount % 2) == 1) { // if odd dot com (place vertically) incr = gridLength; // set vertical increment } while ( !success & attempts++ < 200 ) { // main search loop (32) location = (int) (Math.random() * gridSize); // get random starting point //System.out.print(" try " + location); int x = 0; // nth position in dotcom to place success = true; // assume success while (success && x < comSize) { // look for adjacent unused spots if (grid[location] == 0) { // if not already used coords[x++] = location; // save location location += incr; // try 'next' adjacent if (location >= gridSize){ // out of bounds - 'bottom' success = false; // failure } if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge success = false; // failure } } else { // found already used location // System.out.print(" used " + location); success = false; // failure } } } // end while int x = 0; // turn good location into alpha coords int row = 0; int column = 0; // System.out.println("/n"); while (x < comSize) { grid[coords[x]] = 1; // mark master grid pts. as 'used' row = (int) (coords[x] / gridLength); // get row value column = coords[x] % gridLength; // get numeric column value temp = String.valueOf(alphabet.charAt(column)); // convert to alpha alphaCells.add(temp.concat(Integer.toString(row))); x++; // System.out.print(" coord "+x+" = " + alphaCells.get(x-1)); } // System.out.println("/n"); return alphaCells; } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五华县| 富源县| 定兴县| 泰安市| 子长县| 乌拉特后旗| 渭南市| 晋江市| 安龙县| 浙江省| 湾仔区| 门源| 略阳县| 修武县| 丰顺县| 正镶白旗| 固原市| 长寿区| 油尖旺区| 岳普湖县| 仁怀市| 黄龙县| 榆林市| 伊宁县| 铁岭县| 磐安县| 惠州市| 富宁县| 石渠县| 高台县| 康保县| 潼关县| 桐柏县| 峨边| 瓦房店市| 天全县| 图片| 图片| 安泽县| 绥棱县| 焦作市|