繁体   English   中英

java-程序执行

[英]java - execution of program

我目前正在学习如何编程,并且我正在尝试向其添加功能的程序。 我想知道您是否可以告诉我所需的代码在哪里才能正确执行。 谢谢

//---------------------------------------------------------------------
// 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();
}

再次感谢。 非常感谢帮助

只需将代码添加到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
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM