349989153 / 349989153.github.io

My personal blog.
0 stars 0 forks source link

Head First Java 中文版--泳池迷宫 #10

Open 349989153 opened 4 years ago

349989153 commented 4 years ago

20200806093954

349989153 commented 4 years ago

题目就长如上那样。

答案是:

import java.io.*;
class test  
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int x = 0;
        while (x < 4) {
            System.out.print("a");
            if (x < 1) {
              System.out.print(" ");
            }
            System.out.print("n");
            if (x < 1) {
                System.out.print("oise");
            }
            if (x > 1) {
                x = x + 2;
            }
            if (x == 1) {
                System.out.print("noys");
            }
            if (x > 3) {
                System.out.print(" oyster");

            }
            System.out.println("");
            x = x + 1;
        }
    }
}
349989153 commented 4 years ago

基本上不是靠推理得出来的。

而是分模块试的,比如这道题,有x判断模块(x > 0之类)、x赋值模块(x = x + 1之类)、打印模块(print之类)。先把x判断模块填上去,打印模块随便填填,有区别度就行。

然后调整x判断模块的同时也微调x赋值模块,先保证搞出3行输出,那基本就没错了。

花了我一个多小时的时间,没啥思考的成就感,觉得浪费时间而已。