2017-04-19 74 views
0

我目前正在学习如何编程,我有一个程序,我正在尝试添加一个功能。我想知道是否可以告诉我所需的代码需要去哪里才能正确执行。谢谢java - 程序的执行

//--------------------------------------------------------------------- 
// 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

最好的学习方法是尝试一些不起作用的东西,并猜测出错的原因!只要您备份您的更改,实验就没有任何坏处。 – Tashi

+0

这些是新的行为(方法),所以将它们添加到main方法之后,只需传递正确的参数即可直接调用它们 –

回答

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 
    } 

}