HQ9+修正版

前回のポストでHQ9+をJavaで実装してみたのですが、
Qでの挙動をまるっきり勘違いしていました。

誤:Qは一回しか利用されない

正:そんな制限どこにもありませんでしたorz


修正版のHQ9クラスを張っておきます。

public class Hq9 {

    private String source;
    private int count = 0;

    public int getCount() {
        return count;
    }

    public void setCount() {
        this.count++;
    }

    public void getSource() {
        System.out.print(this.source);
    }

    public void setSource(String source) {
        this.source = source;
    }

    public void hello() {
        System.out.println("Hello world!");
    }

    public void bottles() {
        for (int i = 99; i >= 0; i--) {
            switch (i) {
                case 2:
                    System.out.println("2 bottles of beer on the wall, 2 bottles of beer.");
                    System.out.println("Take one down and pass around, 1 bottle of beer on the wall.");
                    System.out.println("");
                    break;
                case 1:
                    System.out.println("1 bottle of beer on the wall, 1 bottle of beer.");
                    System.out.println("Take one down and pass around, no more bottles of beer on the wall.");
                    System.out.println("");
                    break;
                case 0:
                    System.out.println("No more bottles of beer on the wall, no more bottles of beer.");
                    System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
                    break;
                default:
                    System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer.");
                    System.out.println("Take one down and pass around, " + (i - 1) + " bottle of beer on the wall.");
                    System.out.println("");
            }
        }
    }
}


教訓:思い込みで実装しては駄目