你的位置:首页 > Java教程

[Java教程]要进村啦


终于可以写有一点意思的代码。接下来,会通过几个小例子,来学习java,不过开始之前,先说说按什么步骤来写程序, 每个人都有自己的开发习惯,此处也并非标准的步骤,所以此处也只是对像我一样的小白的一个参考。

按照惯例,看看这次的内容:


二、为什么要先写测试代码呢?

这个思想来源于极限编程,Extreme Programming(简称XP),这样做可以是你编程更快更简单,我感觉还可以。通过测试代码,你知道的类的方法需要完成什么样功能才算合格,这就很简单啦,你只需让你的编写的类通过测试代码就可以啦,而且,以后有更好的技术实现当下功能时,不怕修改完代码,出现bug,只需通过你原来的测试就行啦,所谓的单元测试好像就是这样的。


 

三、一个小例子践行上边的编程思想

为什么不说XP了呢,这个不是一时能说完的,可以自己查资料。

写一个DotCom游戏,当然没有期望任何的图形界面,新手总是这样干,原来我也是如此啊,以后会有图形界面程序的。java是一种后端语言,组织着一个项目的逻辑关系,所以,还是多练练逻辑吧,不要妄想着用纯java做一个精美的界面。

这是一个攻击达康网络的游戏,感觉好高大上啊,然而它就是一个猜数字的小游戏。在7 x 7的格子上有三个达康公司,每个达康公司,占三个连续的格子。他们的位子随机生成,你通过猜(A0, 或F5),来判断你是否击中目标,没猜中,结果为"miss", 猜中"hit",如果把一个DotCom的三个坐标都猜中,结果为"kill".

一步一步来:

这个程序最主要的一个类是是什么?当然是这个DotCom啦。

它有什么主要的功能呢?是击打的对象,有自己的坐标,和检验自己是否被击中。

所以它的属性是:储存所在格子信息的 int[] localCells数组,和一个记录自己被击中几次的属性,int numOfHits。方法:检查自己是否被击中的方法:checkYourself(), 它是有参数的来接受玩家的猜测值,并且有返回结果。所以, String checkYourself(int userGuess).

总结一下主要的属性和方法:

  • attributes:int[] lockaCells ; int numOfHits;
  • methods:String checkYourself(int userGuess).

 当然,还有其他的方法,比如对属性的封装方法,setter(), getter(),这些都不是brain method,所以不提啦。

把主要的方法的伪码写出来:

 1 //不是正规的伪码,讲清楚流程就行 2 checkYourself(userGuess): 3   result = "miss" 4   for(i:localCells)//循环,一一判断userGuess是否等于localCells中的值 5     if userGuess == i 6       numOfHits += 1 7       result = "hit" 8       break 9   if numOfHits == 310     result = "kill"11   return result

 

好啦,写real codes,but test codes firstly.

 1 public class Test { 2   public static void main(String[] args) { 3     SimpleDotCom dotCom = new SimpleDotGom(); 4     int[] localCells = {2, 3, 4};//指定DotCom的位置,真实游戏中,是随机生成的 5     dotCom.setLocalCells(localCells); 6     int guess = 2;//玩家猜的数字 7     String result = dotCom.checkYourself(guess);//测试的方法 8     System.out.println("result = " + result); 9   }10 }

 

 好啦,写SimpleDotCom:

 1 public class SimpleDotCom { 2   private int[] localCells; 3   private int numOfHits; 4   public void setLocalCells(int[] localCells) { 5     this.localCells = localCells; 6   } 7   public String checkYourself(int userGuess) { 8     String result = "miss"; 9     for(int i : localCells) {10       if ( i == userGuess) {11         result = "hit";12         numOfHits ++;13         break;14       }15     }16     if (3 == numOfHits){//这是一个编程规范的一个小细节,不要不注意哦17       result = "kill";18     }19       return result;20   }21 }

 

 看结果:

果然如自己所料啊,今天就到这里。其实这个程序bug多多,你可以试着找找并改改哈,下次,写个更好的。


 

说个细节问题。

大家都知道判断一个数是奇偶的方法,两种最常用的,num % 2 == 0 or num % 2 == 1,可能大家会有点疑惑,这不一样吗?是吗?

结果就是不一样:

但是python没有这个问题:

这是语言特性决定的,所以,以后要用 num % 2 == 0 来判断,不要在用 num % 2 == 1啦,这就是细节的力量。


 每日一句:

Life doesn't just happen for you; you receive everything in your life based on what you've given.

你今天的一切源于昨天,同样你明天的一切取决于今天的表现。

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0//wAARCADcANwDASIAAhEBAxEB/8QAGwABAAIDAQEAAAA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