[关闭]
@Senl 2017-03-21T16:45:52.000000Z 字数 2002 阅读 1205

3.21 学习进度

技术学习



3.21 学习进度

JAVA

JAVA 开发类的主要步骤(HeadFirst Java P.99)

  1. 找出类应该做的事情
  2. 列出实例变量和方法
  3. 编写方法的伪代码
  4. 编写方法的Tset程序
  5. 实现类
  6. 测试方法
  7. debug or refactor
  8. 邀请一个辣妹参加庆功派对(Nonsense)

一个战舰是否击沉的游戏(HF Java第五章实战)

  1. //战舰生成和玩家猜测类
  2. //主要是生成一个一维9个格子的战场,有一个长度为两格的战舰,玩家需要猜测它的位置并击中它
  3. public class SimpleDot {
  4. int[] locationCells;
  5. int num_Hits = 0;
  6. public void setLocationCells(int[] locs){
  7. locationCells = locs;
  8. }
  9. public String checkYourself (String stringGuess){
  10. int guess = Integer.parseInt(stringGuess);
  11. String result = "miss";
  12. for (int cell : locationCells){
  13. if (guess == cell){
  14. result = "hit";
  15. num_Hits ++;
  16. break;
  17. }
  18. }// break for loops
  19. if(num_Hits == locationCells.length){
  20. result= "kill";
  21. }
  22. System.out.println(result);
  23. return result;
  24. }
  25. }
  26. //处理玩家输入的类(搬书上的砖的,大概理解就是如果成功输入就将结果传入,如果不是就为异常,并暂停运行,然后还有异常处理状态这个东西)
  27. import java.io.*;
  28. public class GameHelper {
  29. public String getUserInput(String prompt){
  30. String inputLine = null;
  31. System.out.println(prompt + " ");
  32. try{
  33. BufferedReader is = new BufferedReader(
  34. new InputStreamReader(System.in));
  35. inputLine = is.readLine();
  36. if(inputLine.length() == 0)
  37. return null;
  38. }catch(IOException e){
  39. System.out.println("IOException:" + e );
  40. }
  41. return inputLine;
  42. }
  43. }
  44. //游戏启动类,处理是否击中和修改战舰是否存活这样的东西
  45. //其实游戏中存在一个很重大的bug,连续两次击中已知的战舰格子,战舰也会死...
  46. //没有限制用户输入的格子数,万一输入的格子数是999怎么办
  47. //明天尝试在Helper那里修一下,或者在Drive那
  48. public class SimpleDotDrive {
  49. public static void main (String[] args){
  50. int num_Guess = 0;
  51. GameHelper helper = new GameHelper();
  52. SimpleDot theDot = new SimpleDot();
  53. int randomNum = (int) (Math.random()*8 )+1;
  54. int[] locations = {randomNum,randomNum+1};
  55. theDot.setLocationCells(locations);
  56. boolean isAlive = true ;
  57. while(isAlive == true) {
  58. String guess = helper.getUserInput("enter a number");
  59. String result = theDot.checkYourself(guess);
  60. num_Guess ++;
  61. if(result.equals("kill")){
  62. isAlive = false;
  63. System.out.println("YOU took "+num_Guess+" times to guess");
  64. }
  65. }
  66. }
  67. }

Android

C语言及其他计算机知识(如:数据结构/算法/计网等)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注