我目前正在学习如何编程,我正在尝试添加一项功能。我想知道你是否可以告诉我所需的代码需要去哪里才能正确执行。谢谢
//---------------------------------------------------------------------
// GolfApp2.java
//
// Allows user to enter golfer name and score information.
// Displays information ordered by score.
//----------------------------------------------------------------------
import java.util.Scanner;
import ch08.trees.*;
import support.*; // Golfer
public class GolfApp2
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
String name; // golfer's name
int score; // golfer's score
BSTInterface<Golfer> golfers = new BinarySearchTree<Golfer>();
Golfer golfer;
int numGolfers;
String skip; // Used to skip rest of input line after reading integer
System.out.print("Golfer name (press Enter to end): ");
name = conIn.nextLine();
while (!name.equals(""))
{
System.out.print("Score: ");
score = conIn.nextInt();
skip = conIn.nextLine();
golfer = new Golfer(name, score);
golfers.add(golfer);
System.out.print("Golfer name (press Enter to end): ");
name = conIn.nextLine();
}
System.out.println();
System.out.println("The final results are");
numGolfers = golfers.reset(BinarySearchTree.INORDER);
for (int count = 1; count <= numGolfers; count++)
{
System.out.println(golfers.getNext(BinarySearchTree.INORDER));
}
}
}
这是添加
所需的代码int countLess ( BinarySearchTree<Golfer> tree, Golfer maxValue )
{
BSTNode<Golfer> maxNode = tree.root;
BSTNode<Golfer> minNode = tree.root;
int count = 0;
//Traverse Right Sub tree
while(maxNode!=null)
{
if( maxNode.getInfo() < maxValue){
count ++;
}
maxNode = maxNode.getRight();
}
//Traverse Left subtree
while(minNode!=null)
{
if( minNode.getInfo() < maxValue){
count ++;
}
minNode = minNode.getLeft();
}
return count;
}
另一个
Golfer min(BinarySearchTree<Golfer> tree) {
BSTNode<Golfer> minNode = tree.root;
if (minNode == null) {
return null;
}
while (minNode.getLeft() != null) {
minNode = minNode.getLeft();
}
return minNode.getInfo();
}
再次感谢。非常感谢帮助
答案 0 :(得分:0)
只需在GolfApp2类中添加代码即可。它只是一个函数,你可以用GolfApp2对象调用它。
public class GolfApp2 {
public static void main(String[] args)
{
//Code
// call thoes methods here by creating object to the class
GolfApp2 obj = new GolfApp2();
int returnedValue = obj.countLess (param1,param2);
}
int countLess ( BinarySearchTree<Golfer> tree, Golfer maxValue )
{
//code
}
Golfer min(BinarySearchTree<Golfer> tree) {
//code
}
}