第五天,也就是书中的第五章,这一章内容是编写程序,在了解前四章的基础知识,现在是要把知识开始运用,在以后的工作和学习中,你会碰到各种各样的需求,那么碰到客户的需求你该怎么把他转换成我们实现的代码呢?第五章中就有教我们设计思路,首先知道下需求,我们是要设计一个类似战舰攻击的游戏
上面是流程图 下一页类的设计思路,首先这章我们先开发给简单版本。
在书中,教我们先写伪代码,测试码,真实码,第一步的伪代码就不是代码,可以是文字,写出你需要的类,变量,方法,详细内容如下图
一 :在我的设计中,我觉得这个至少需要两个类,一个是战舰的类,里面应该保护了战舰的位置(locationCells),击中次数两个变量(numOfHits ),以及设置战舰位置(setLocationCells),检查战舰是否被击中(checkYourself)两个方法。一个是执行类,包含了main方法,方法中调用给战舰赋值位置,然后输入猜的值,调用是否击中的方法。上面所写的就是伪代码
class SimpleDotCom{//战舰类
int[] locationCells;//定义战舰位置 以数组形式
int numOfHits = 0;// 定义击中次数
public void setLocationCells(int[] locs){//战舰位置的set方法
locationCells = locs;//
}
public String checkYourself(String stringGuess){//检查是否被击中的方法
int guess = Integer.parseInt(stringGuess);//将传进来的实参转换为int类型 并赋值给guess
String result = "miss";//定义局部变量 结果为miss
for(int cell : locationCells){// for循环 判断战舰位置数组中每一个值 有没有跟guess相同
if(guess == cell){//如果有相同
result = "hit";//结果 改为hit
numOfHits++;//实例变量 击中次数加1
break;//
}
}
if(numOfHits == locationCells.length){//当击中次 与战舰总长度相等时
result = "kill";//将结果变成击杀
}
System.out.println(locationCells.length);//输出 战舰长度
System.out.println(result);//输出结果
return result;// 返回结果 因为checkYourself这个方法是有返回的 所以得返回值
}
}
上面是定义好的战舰类
public class SimpleDotComTestDrive1{//执行类
public static void main (String[] args){//主函数
SimpleDotCom dot = new SimpleDotCom();//定义个 战舰对象
int[] locations = {2,3,4};//给数组location 赋值 假数据
dot.setLocationCells(locations);//给战舰位置赋值 location 假数据
String userGuess = "2";// 定义猜的数据
String result = dot.checkYourself(userGuess);//调用检查是否击中的方法,并将假数据传过去
}
}
二:上面执行类和战舰类,数据都是写死的假数据,书中称为测试码
执行结果如下图
三:真实码跟测试码还有些区别,战舰的位置,跟用户猜的数据,这两个数据应该都是随机和用户自己猜的,战舰位置随机好解决,用户猜的数据怎么来呢?我们应该还要定义个用户猜的类
import java.io.*;//引用包
class GameHelper{//定义个用户输入的类
public String getUserInput(String promt){//获取用户输入值 int类型
String inputLine = null;//定义个变量
System.out.print(promt + " ");//输出实参
try{//异常捕捉 不做扩展 能用就好
BufferedReader is = new BufferedReader(//定义一个读取BufferedReader 对象
new InputStreamReader(System.in));//
inputLine = is.readLine();//获取屏幕上数据 赋值给inputLine
if(inputLine.length() == 0 )return null;//
}catch(IOException e){//
System.out.println("IOException : " + e);//
}//
return inputLine;
}//
}
完整的代码
import java.io.*;
public class SimpleDotComTestDrive2{//执行类
public static void main (String[] args){
System.out.println("深海中有一艘战舰 请输入1-20数字 击中三次就能击沉战舰");
int numOfGuesses = 0;//记录玩家猜测次数的变量
GameHelper helper = new GameHelper();//玩家输入的类
SimpleDotCom theDotCom = new SimpleDotCom();//新建一个战舰的类
int randomNum = (int)(Math.random()*20);//用随机数产生战舰第一个位置
System.out.println("随机数是" + randomNum);
int[] locations = {randomNum,randomNum+1,randomNum+2};//用随机数产生的三个数组成一个数组 作为战舰
theDotCom.setLocationCells(locations);//给战舰赋值
boolean isAlive = true;//一个用于判断是否击沉战舰的标识符
while(isAlive == true){//只要战舰还存在 就一直执行判断
String guess = helper.getUserInput("请输入数字 : ");//获取玩家输入字符串
String result = theDotCom.checkYourself(guess);//检查玩家的猜测 并将方法返回的结果保存到result内
numOfGuesses++;
if(result.equals("击沉")){//判断是否击沉 如果击沉改变标识符 输出使用了几次击沉
isAlive = false;
System.out.println("你使用了" + numOfGuesses + " 次击沉战舰");
}
}
}
}
class SimpleDotCom{
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs){
locationCells = locs;
}
public String checkYourself(String stringGuess){
int guess = Integer.parseInt(stringGuess);
String result = "未击中";
for(int cell : locationCells){
if(guess == cell){
result = "击中";
numOfHits++;
break;
}
}
if(numOfHits == locationCells.length){
result = "击沉";
}
System.out.println("你输入数字的结果是 : "+result);
return result;
}
}
class GameHelper{
public String getUserInput(String promt){
String inputLine = null;
System.out.println(promt + " 5555");
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;
}
}
运行结果
总结下 今天的主要是设计思路,新的知识点不多,有一个获取随机数,一个强转类型,一个引用第三方的包