でアクションを実行するためにどのように私は、各ノード(GameEntry
クラス)が一つの「ゲームプレイ」(名/スコアのペア)を表し、バイナリ検索ツリーを持っています。ツリーは名前(スコアではなく)で整理されています。私はツリーのトップ10のスコア(対応する名前)のリストを印刷するためのメソッドを記述しようとしています。私は木を再帰的に横断することを考えました。ノードを配列(ScoreBoard
クラス)に置くのは、それが高いスコアである場合のみです。私の問題を除いて、スコアボードが再帰の途中ですべてのステップを印刷するということを除いて、動作します。再帰は - だけで終わり
public void printTopTen()
{
ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10
printTopTenRecur(this.root, board);
}
// Auxillary method for printTopTen()
private void printTopTenRecur(GameEntry node, ScoreBoard board)
{
if (node == null)
{
return;
}
printTopTenRecur(node.getLeft(), board);
board.add(node); // adds the node to the scoreboard if it's a high score
System.out.println(board);
printTopTenRecur(node.getRight(), board);
}
私は考えることができる唯一の事は、クラス(board
と呼ばれる)の属性を作成し、再帰が行われた後、属性をプリントアウトすることでした。しかし、私はコンパイル時エラーvoid cannot be converted to String
を得ています。私はそれをどうやって行うべきか分かりません。
public String printTopTen()
{
ScoreBoard board = new ScoreBoard(10); // new scoreboard with capacity of 10
printTopTenRecur(this.root, board);
return System.out.println(this.board);
}
// Auxillary method for printTopTen()
private void printTopTenRecur(GameEntry node, ScoreBoard board)
{
if (node == null)
{
return;
}
printTopTenRecur(node.getLeft(), board);
board.add(node); // adds the node to the score board if it's a high score
this.board = board; // assign local board to the board on the tree
printTopTenRecur(node.getRight(), board);
}
ありがとうございます。ご提案ありがとうございます。 'println'がvoidを返すようになりました。あなたの修正後、私は 'return this.board.toString()'も試みました。それはコンパイルされますが、ランタイムエラーNoSuchMethodErrorが発生します。もう一度混乱した。 – yroc