最後のcountのmethod はinteger を返します。それはこのタワーのブロックの数と同じでそしてこのmethod が受けっとている、charとも同じと自分は理解しているのですが。自分の英語の理解は正しいのでしょうか?あるプログラムがチェックすると
2が返ってくるはずが 5が出ていますと出ます。どうしてでしょうか?
java
1 2package week03; 3 4/** 5 * A recursive representation of a tower of blocks. 6 * 7 * @author Michael Albert 8 */ 9public class Tower{ 10 11 /** The top block. */ 12 private char top; 13 14 /** The rest of the tower. */ 15 private Tower rest; 16 17 /** 18 * Creates a new empty Tower. 19 */ 20 public Tower() { 21 this.top = ' '; 22 this.rest = null; 23 } 24 25 /** 26 * External classes can only create empty towers and manipulate 27 * them using public methods, because this constructor is 28 * private. 29 * @param top the top block in this tower 30 * @param rest the rest of the tower 31 */ 32 private Tower(char top, Tower rest) { 33 this.top = top; 34 this.rest = rest; 35 } 36 37 /** 38 * Returns true if this tower is empty, otherwise false. Empty 39 * towers are represented with the top block being a space 40 * character. 41 * @return whether the tower is empty or not. 42 */ 43 public boolean isEmpty() { 44 return top == ' '; 45 } 46 47 /** 48 * Creates a new tower by adding the given block to the top of 49 * this tower. 50 * @param block a block to add to the top of this tower. 51 * @return a new tower created by adding a block to the top of 52 * this tower. 53 */ 54 public Tower add(char block) { 55 return new Tower(block, this); 56 } 57 /** 58 * A method that returns an int equal to the height, 59 * i.e., number of blocks, in the tower. 60 * @return an int equal to the height. 61 */ 62 public int height(){ 63 int count =0; 64 if (!(this.isEmpty())){ 65 count ++; 66 if (!(this.rest.isEmpty())){ 67 count += this.rest.height(); 68 69 } 70 71 } 72 return count; 73 74 } 75 /** 76 * A method that returns an int equal to the number of blocks equal to c in the tower. 77 * @return int equal to the number of blocks in the tower. 78 */ 79 80 public int count(char c){ 81 int count =0; 82 83 if (this.top == c){ 84 count = this.height(); 85 86 } 87 88 return count; 89 90 91 } 92 93 94 95 96 97 98} 99
回答1件
あなたの回答
tips
プレビュー