1章 Javaの世界に飛び込もう

Head First Java 第2版 ―頭とからだで覚えるJavaの基本

Head First Java 第2版 ―頭とからだで覚えるJavaの基本


スタートです!
jsのときもそうだったのですが、;を打つタイミングがイマイチ分かってないですね。
…と書いたところで、説明を見つけました。各ステートメントの末尾ですね。

BeerSong

初コンパイル&実行です。

public class BeerSong{
	public static void main (String [] args){
		int beerNum = 99;
		String word = "bottles";
		
		while (beerNum > 0){
		
			if (beerNum ==1){
				 word ="bottle";
			}
			System.out.println(beerNum +" "+ word + "of beer on the wall");
			System.out.println(beerNum +" "+ word + "of beer.");
			System.out.println("Take one down.");
			System.out.println("Pass it around.");
			beerNum = beerNum -1;
			
			if (beerNum > 0){
				System.out.println(beerNum +" "+ word + "of beer on the wall");
			}else{
				System.out.println("No more bottles of beer on the wall");
			} //else end
		} // while end
	} // main end
} // class end

コマンドプロンプトを立ち上げ、

f:id:nehalemv:20120321171209p:image

幾度かのエラーを修正すると、コンパイルできました。
実行。

f:id:nehalemv:20120321171204p:image

成功b

PhraseOMatic

ふたつめ。
配列の中から単語をランダムで選んで表示させるもの。
ビジネスなワードですね。

public class PhraseOMatic{
	public static void main (String [] args){
		
		String [] wordListOne ={
			"24/7","multi-Tier","30,000 hoot","B-to-B","win-win","front-end","web-based","pervasive","smart","six-sigma","critical-path","dynamic"
		};
		String [] wordListTwo ={
			"empowerd","sticky","value-added","oriented","centric","distributed","clustered","branded","outside-the-box","positioned","networked","focused","leveraged","aligned","targeted","shared","cooperative","accelerated"
		};
		String [] wordListThree ={
			"process","tipping-point","solution","architectre","core competency","strategy","mindshare","portal","space","vision","paradigm","mission"
		};
		
		int oneLength = wordListOne.length;
		int towLength = wordListTwo.length;
		int threeLength = wordListThree.length;
		
		int rand1 = (int) (Math.random()*oneLength);
		int rand2 = (int) (Math.random()*towLength);
		int rand3 = (int) (Math.random()*threeLength);
		
		String phrase = wordListOne [rand1] + " " + wordListTwo [rand2] + " " + wordListThree [rand3];
		
		System.out.println("What we need is a " + phrase);
	}
}

これをコンパイル。

f:id:nehalemv:20120321180104p:image

単語の間のスペースが欠けていたので修正。

f:id:nehalemv:20120321180103p:image

プールパズル

一時間ぐらい悩んで結局正解を見てしまった、穴埋めパズル。
これが解答ソース

class PoolPuzzleOne{
	public static void main(String [] args){
		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("oyster");
				x = x + 2;
			}
			if(x == 1){
				System.out.print("noys");
			}
			if(x < 1){
				System.out.print("oise");
			}
			System.out.println("");
			x = x + 1;
		}
	}
}

しかし、これで実行するとこうなります。

f:id:nehalemv:20120322160418p:image

解答としては、

a noise
annoys
an oyster

と表示されなければいけません。
Head First Labs from O'Reilly Media, Inc. :: Head First Java, Second Edition
こちらで入手できるファイルにも、この設問の解答ファイルはありません。
よって、手打ち入力が正しいということにします。

ルールでは、選択肢のコードの複数回使用を禁じています。
すべてのコードを使う必要はないようです。

問題は『三行目に半角スペースを入れるかどうか』です。
一行目に半角スペースのコードは使ってしまっています。
三行目にも半角スペースを入れるのなら、一行目で発生させた半角スペースのコードを『再利用』する形で実現を目指すのだろうと考えました。
ということは、xに入っている値を下げたり上げたりするのだろうと思いました。
しかし、今回の設問制約の中で考えていくと、どうも難しい。
しぶしぶ解答をみて手打ちしたところ、半角スペースがうまくいかない。
検索してみるとこんなページが。

P24 プールパズルの解答の実行結果が違う
どうしても解けなかったのでみたら、そもそも問題が間違ってるという仕打ち。

a noise
annoys
an oyster

が、本での出力結果だが、解答をプログラムして実行すると、

a noise
annoys
anoyster

間違ってる問題を正しく答えるのは無理だ。

駆け出しプログラマーのJava奮闘記 Head First Java5章までの感想
┌┴┐┌┴┐┌┴┐ -┼-  ̄Tフ ̄Tフ __ / /
  _ノ   _ノ   _ノ ヽ/|    ノ    ノ       。。
       /\___/ヽ
    /ノヽ       ヽ、
    / ⌒''ヽ,,,)ii(,,,r'''''' :::ヘ
    | ン(○),ン <、(○)<::|  |`ヽ、
    |  `⌒,,ノ(、_, )ヽ⌒´ ::l  |::::ヽl  
.   ヽ ヽ il´トェェェイ`li r ;/  .|:::::i |
   /ヽ  !l |,r-r-| l!   /ヽ  |:::::l |
  /  |^|ヽ、 `ニニ´一/|^|`,r-|:「 ̄
  /   | .|           | .| ,U(ニ 、)ヽ
 /    | .|           | .|人(_(ニ、ノノ