shokosブログ

プログラミング

Interpreterかいた!

HQ9+かきました
HQ9+ - Wikipedia

package jp.ne.hatena.syoko_sasaki;

/**
 * Hello world!
 *
 */
public class HQ9Plus {
	public static void main(String[] args) {
		Interpreter interpreter = new Interpreter(args[0]);
		interpreter.run();
	}

	static class Interpreter {
		private String source;
		private int count = 0;
		public Interpreter(String source) {
			this.source = source;
		}

		public void run() {
			for (char c : source.toCharArray()) {
				switch (c) {
				case 'H':
					printHelloWorld();
					break;
				case '9':
					printBottles();
					break;
				case 'Q':
					System.out.println(source);
					break;
				case '+':
					count++;
					break;
				}
			}
		}

		private void printHelloWorld() {
			System.out.println("Hello World!");
		}

		private void printBottles() {
			for (int i = 99; i >= 0; i--) {
				String bottle = String.valueOf(i) + " bottle";
				String afterBottle = String.valueOf(i - 1) + " bottle";
				if (i == 2) {
					bottle += "s";
				} else if (i != 1) {
					bottle += "s";
					afterBottle += "s";
				} else {
					afterBottle = " no more bottles";
				}
				if (i != 0) {
					System.out.println(bottle + " of beer on the wall, "
							+ bottle + " bottles of beer.");
					System.out.println("Take one down and pass it around , "
							+ afterBottle + " bottles of beer on the wall.");
					System.out.println("");
				} else {
					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.");
				}
			}

		}

	}
}

ちゃんと実行できた!