《Head Frist Java》

写一个猜数游戏

一.实现这样一个数字炸弹游戏要达成的目标(这个游戏怎么玩)

在电脑给出的一组数中,用户输入一个数,如果这个数在电脑给的数组内,则报出“miss”,如果不在,则报出“miss”,如果用户把这一组数的数字都猜对了,则报出“kill”并结束游戏。

 

二:开发一个类(class)

开发一个类需要三步,预编码(prep code)、测试编码(test code)和正是编码(real code)

1.预编码是伪代码的一种,介于逻辑思路和正式编码中的一中形式,强调编码的逻辑,而不注重语法的形式

2.测试编码,用于测试正式编码的准确性

3.正式编码,是我需要编译并且能够稳定运作的代码

极限编程(它的存在形式和方法是怎么样的?

 

对checkYourself的测试代码

public class SimpleDotCom {
    int[] locationCells;                                //输入locationcells
    int numOfHits = 0;                                  //从0开始设置hit的次数

    public void setLocationCells(int[] locs) {          //声明一个setLocationCells(**)
        locationCells = locs;                           //将locs赋值给locationCells
    }                                                   //结束声明

    public String checkYourself(String stringGuess) {    //创建一个chekYourself的方法,并创建一个stringGuess的字符串

        int guess = Integer.parseInt(stringGuess);       //将stringGuess用*Integer。parseInt*处理,并将其转化为整数形式
        String result = "miss";                          //将“miss”赋给result
        for (int cell : locationCells) {                 //创建一个for循环,括号里是遍历locationCell的元素**
            if (guess == cell) {                         //创建一个if判断,对比guess的元素是否等于cell中的元素
                result = "hit";                          //如果if判断为真则将“hit”赋给result
                numOfHits++;                             //并且给NumOfHits加1
                break;                                   //跳出if判断
            }                                            //结束if判断
        }                                                //结束for循环
        if (numOfHits == locationCells.length) {         //创建新的if判断(如果numOfHits的数等于locationCells的长度)
            result = "kill";                             //则将“kill”赋给result
        }                                                //结束if判断
        System.out.println(result);                      //输出result
        return result;                                   //返回result**
    }

    public class SimpleDotComTestDrive {

    }

    public static void main(String[] args) {        //创捷测试代码test code及入口
        SimpleDotCom dot = new SimpleDotCom();      //实例化SimpleDotCom对象
        int[] locations = {2, 3, 4};                  //为DotCom创建整数数组
        dot.setLocationCells(locations);            //在dot上调用setter方法,对目的值(locations)修改成Locationcells
        String userGuess = "2";                     //假设用户猜测的数字为2
        String result = dot.checkYourself(userGuess);//为DotCom调用checkYourself方法并把猜测的数字传递给它,将这个结果赋给result
        String testResult = "failed";                //将“failed”赋给testResult
        if (result.equals("hit")) {                   //创建一个if判断,***
            testResult = "passed";                   //将“passed”赋给testResult
        }                                            //结束if判断
        System.out.println(testResult);              //输出testResult结果
    }                                                //结束测试代码
}                                                    //结束整个代码

现在需要完善这个游戏,我们要如何让电脑连续输入三次数字,然后输出kill?

就是我们下一章节要解决的问题

posted on 2022-07-04 11:51  葉蓻蔿  阅读(23)  评论(0编辑  收藏  举报

导航