2

これはメインクラスです:

import javax.swing.*;
class BinarySearchTree {
private Node root;

public void main()
{
    int Value = 0;
    while(Value!= -1)
    {
        Value = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));
        insert(root, Value); 
    }
    print();
}

public void insert(Comparable x) 
{
    root = insert(root, x);
}

public boolean find(Comparable x) 
{
    return find(root,x);
}

public void print() 
{
    print(root);
}

@SuppressWarnings("unchecked")
boolean find(Node tree, Comparable x) 
{
    if (tree == null)
        return false;

    if (x.compareTo(tree.data) == 0) 
        return true;

    if (x.compareTo(tree.data) < 0)
        return find(tree.left, x);
    else
        return find(tree.right, x);
}

void print(Node tree) 
{
    if (tree != null) 
    {
        print(tree.left);
        System.out.println(tree.data);
        print(tree.right);
    }
}

@SuppressWarnings("unchecked")
Node insert(Node tree, Comparable x) 
{
    if (tree == null) 
    {
        return new Node(x);
    }

    if (x.compareTo(tree.data) < 0) 
    {
        tree.left = insert(tree.left, x);
        return tree;
    } 
    else 
    {
        tree.right = insert(tree.right, x);
        return tree;
    }
}

}

ノード クラス:

public class Node {
    public Comparable data;
    public Node left;
    public Node right;

    Node(Comparable newdata) {
        data = newdata;
    }
}

「print();」を呼び出すと、結果を印刷しようとします。void メイン クラスで、ツリーにすべての値を挿入した後、何も出力しません。各メソッドを個別に呼び出すと機能しますが、メイン クラスから呼び出そうとすると機能しません。これが起こっている理由は何ですか?どうもありがとう

4

1 に答える 1

4

It is because you dont have a main method. Change the signature of your main method to:

public static void main(String[] args)

and re run it.

于 2012-04-11T01:10:26.150 に答える