4.9 解决问题

博客介绍了软件生命周期,包括需求分析和定义、设计、实现、测试、维护。以Twenty - One Pickup为例,阐述了各阶段内容,如设计伪代码、实现程序等。还介绍了测试方法,如语句覆盖和分支覆盖,指出完整语句覆盖不能保证程序正确性。
软件生命周期:需求分析和定义、设计、实现、测试、维护。

4.9.1 Twenty-One Pickup:需求分析和定义

4.9.2 Twenty-One Pickup:设计

写出伪代码,设计出需要写的方法。

4.9.3 Twenty-One Pickup:实现

TwentyOnePickup.java

// TwentyOnePickup.java

import tio.*;

public class TwentyOnePickup {
    /**
     * Play the game of Twenty-One Pickup. The user and the computer
     * take turns removing from 1 to 3 stones from a pile. There are
     * 21 stones in the pile to start with. The last one to remove a
     * stone wins.
     */
    public static void main(String[] args) {
        printInstructions();
        // create the initial pile with 21 stones
        int numberOfStones = 21;
        // keep track of who moved last
        boolean playerMovedLast = false;
        while (numberOfStones > 0) {
            numberOfStones = playerMove(numberOfStones);
            playerMovedLast = true;
            if (numberOfStones > 0) {
                numberOfStones = computerMove(numberOfStones);
                playerMovedLast = false;
            }
        }
        // print the outcome
        if (playerMovedLast)
            System.out.println("Congratulations, you won.");
        else
            System.out.println("Better luck next time.");
    }

    /**
     * printInstructions prints the initial instructions
     */
    static void printInstructions() {
        System.out.println("The object of this game is"
                + " to remove the last stone./n"
                + "There are 21 stones in the pile to start with./n"
                + "You may remove from 1 to 3 stones on each move./n"
                + "Good Luck!");
    }

    /**
     * playerMove completes one move by the player.
     *
     * @param numberOfStones
     *            The number of stones reamining in the pile.
     * @return The number of stones remaining after the user's move.
     */
    static int playerMove(int numberOfStones) {
        int move = getUserMove(numberOfStones);

        numberOfStones = numberOfStones - move;
        System.out.println("There are " + numberOfStones
                + " stones remaining.");
        return numberOfStones;
    }

    /**
     * computerMove completes one move by the computer.
     *
     * @param numberOfStones
     *            The number of stones reamining in the pile.
     * @return The numberOfStones remaining after the computer's move.
     */
    static int computerMove(int numberOfStones) {
        int move;

        if (numberOfStones <= 3) {
            move = numberOfStones; /* remove the rest */
        }
        else {
            move = numberOfStones % 4;
            if (move == 0)
                move = 1;
        }
        numberOfStones = numberOfStones - move;
        System.out.println("The computer removes " + move
                + " stones leaving " + numberOfStones + ".");
        return numberOfStones;
    }

    /**
     * getUserMove reads in the user's move, only accepting legal
     * inputs.
     *
     * @param numberOfStones
     *            The number of stones reamining in the pile.
     * @return The number of stones selected for removal by the user.
     */
    static int getUserMove(int numberOfStones) {
        System.out.println("Your move - how many stones"
                + " do you wish to remove?");
        int move = Console.in.readInt();

        while (move > numberOfStones || move < 1 || move > 3) {
            if (numberOfStones >= 3)
                System.out.println("Sorry,"
                        + " you can only remove 1 to 3 stones.");
            else
                System.out.println("Sorry, you can only "
                        + "remove 1 to " + numberOfStones
                        + " stones.");
            System.out.println("How many stones"
                    + " do you wish to remove?");
            move = Console.in.readInt();
        }
        return move;
    }
}


对于程序的分析,可以先在程序开头处设置断点,然后应用Eclipse的Debug as,然后在相应的视图下用F5和F7来仔细分析程序的运行走向。

4.9.4 Twenty-One Pickup:测试

语句覆盖(statement converage):要建立测试情况的集合(即测试集),集合中的所有测试情况执行过后要保证程序中的每条语句都执行至少一遍。但是完整的语句覆盖不能保证程序的正确性。
分支覆盖(branch coverage):要建立测试情况的集合,集合中所有的测试情况执行过后,要保证程序中的每条分支都执行至少一遍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值