Learn Java with teacher Wang three major features (ii): Case quickhit: Game output string

Source: Internet
Author: User

Case Quickhit: Game output string
Teacher: Wang Shaohua QQ Group No.:483773664Learning Goals

Complete the game output string

I. Description of requirements

Output random string in console

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7F/2D/wKiom1cV1W2iMR9CAADRaPsIIa4299.jpg " data_ue_src= "E:\My knowledge\temp\a6456277-2b81-4336-abfb-f5168b15ca2a.jpg" >

Second, the Thinking analysis

Generate string

Output string

return string

Three, the difficulty hint

The Player property in the game class, which represents the player, queries the player's level number, and gets the string length of that level according to the level number to the Levelparam class

String lengths can be fixed by a for loop, while random content can be obtained by obtaining random numbers, while different random numbers correspond to different characters to achieve

Iv. Reference Code
123456789101112131415161718192021222324252627282930313233343536373839 /**     * 输出指定级别规定长度的字符串。     * @return 输出的字符串,用于和用户输入比较     */    public String printStr() {        int strLength = LevelParam.levels[player.getLevelNo() - 1].getStrLength();        StringBuffer buffer = new StringBuffer();        Random random = new Random();        // 1、通过循环生成要输出的字符串        for (int i = 0; i < strLength; i++) {            // 1.1、产生随机数            int rand = random.nextInt(strLength);            // 1.2、根据随机数拼接字符串            switch (rand) {            case 0:                buffer.append(">");                break;            case 1:                buffer.append("<");                break;            case 2:                buffer.append("*");                break;            case 3:                buffer.append("&");                break;            case 4:                buffer.append("%");                break;            case 5:                buffer.append("#");                break;            }        }        // 2、输出字符串        System.out.println(buffer);        // 3、返回字符串用于和玩家输入相比较        return buffer.toString();    }
V. Testing
12345678 public class Test {    public static void main(String[] args) {        Player player = new Player();        player.setLevelNo(2);        Game game = new Game(player);        game.printStr();    }}




From for notes (Wiz)

Learn Java with teacher Wang three major features (ii): Case quickhit: Game output string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.