微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自我的节点实例的变量和对象的内容没有被打印出来

如何解决来自我的节点实例的变量和对象的内容没有被打印出来

注意

程序运行良好,问题是当试图打印字典树时,它不打印单词本身:
+++++西班牙woerterbuch++++++++
Bitte waehlen Sie eine Aktion aus!

  1. Einfuegen 2.Suchen 3.Beenden
    1
    Bitte geben Sie ein Wort ein:
    维亚杰
    Bitte geben Sie die bedeutung ein:
    赖斯
    麦汁:
    bedeutung:Reise
    Bitte waehlen Sie eine Aktion aus!
    1. Einfuegen 2. Suchen 3. Beingden

节点类

public class Node{
    private String word,deFinition;
    private Node left,right;
    
    
    public Node (String w,String def){
        this.word = w;
        this.deFinition = def;
    }
    
    public String getW(){   return this.word;   }
    
    public String getD(){   return this.deFinition; }
    
    public Node getL(){ return this.left;   }
    
    public Node getR(){ return this.right;  }
    
    public void setL(Node l){   this.left = l;  }
    
    public void setR(Node r){    this.right = r;    }
}

树类

public class Tree{
    
    Node root;
    
    // sorting insert
    public void insert(Node newWord){
        
        if ( root == null)  root = newWord;
        
        else insert(root,newWord);
    }
    
    protected void insert(Node temp,Node newWord){ // Hello,Tree
        if (newWord.getW().compareto(temp.getW()) < 0){
            if (temp.getL() == null )   temp.setL(newWord);
            
            else insert(temp.getR(),newWord);
        
        } else{
                if (temp.getR() == null)    temp.setR(newWord);
                
                else insert(temp.getR(),newWord); // "end","Tree"
        }
    }
    
    // Output of the entire tree/ dictionary
    public void print(){
        print(root);
    }
    
    protected void print(Node temp){
        if (temp.getL() != null )   print(temp.getL());
        
        System.out.println("Wort: " + temp.getW());
        System.out.println("bedeutung: " + temp.getD());
        
        if (temp.getR() != null )   print(temp.getR());
    }
    
    public void search(String word){
        
        search(root,word);
        /**
            @return this.deFinition or null 
        */  
    }
    
    protected String search(Node root,String toBeFound){
        if ( root == null)  return null;
        
        if ( root.getW().compareto(toBeFound) == 0){
            System.out.println("bedeutung vom gesuchten Wort: " + root.getD());
            return root.getD();
        }
        
        if (root.getW().compareto(toBeFound) > 0){
            return search(root.getL(),toBeFound);
        }
        
        return search(root.getR(),toBeFound);
    }
    
    // optional
    public void delete(Node word){
    
    }
}

字典类

import java.util.*;

public class Dict{
    static Tree t = new Tree();
    static int menu;
    static String word,deFinition;
    static Scanner s = new Scanner(system.in);
    static boolean programRunning = true;
    
    public static void main(String[] args){
        System.out.println("+++++Spanisch woerterbuch+++++++");
        while ( programRunning ){
            showMenu();
        }
        
        
        
        s.close();
    }
    
    private static void showMenu(){

        System.out.println("Bitte waehlen Sie eine Aktion aus!");
        System.out.printf("1. Einfuegen\t 2. Suchen\t 3. Beenden");
        System.out.println();
        menu = s.nextInt();
    
        switch (menu){
        case 1:
            System.out.println("Bitte geben Sie ein Wort ein:");
            word = s.nextLine();
            s.nextLine();
            System.out.println("Bitte geben Sie die bedeutung ein:");
            deFinition = s.nextLine();
            Node node = new Node(word,deFinition);
            t.insert(node);
            t.print();
            break;
        case 2:
            System.out.println("Bitte geben Sie das zu suchende Wort ein:");
            s.nextLine();
            word = s.nextLine();
            t.search(word);
            break;
        case 3:
            programRunning = false;
            break;
        default:
            System.out.println("Ungueltige Eingabe!");
        }
    }
}

解决方法

相当简单的问题。

Scanner#nextInt()

不读取换行符,你写了

word = s.nextLine();
s.nextLine();

这意味着第一个 nextLine() 将消耗 \n 并输出一个空字符串,而第二个将实际读取您输入的文本,但只是将其丢弃。
因此,交换它们

s.nextLine();
word = s.nextLine();

你就完成了。还请记住存在调试器,如果可能,请尝试使用它们。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。