【问题标题】:java code, no output after compile [closed]java代码,编译后没有输出[关闭]
【发布时间】:2014-09-14 01:04:27
【问题描述】:

当我编译这个 java 代码时,我得到了空白(输出中没有任何内容)。 为什么输出是空白的?代码中的问题是什么? 你看到的代码是关于链表的。 我尝试了很多方法都没有成功,似乎有一些我不知道的东西。 我真的很感谢你的帮助。 谢谢。

public class Node {

private String data;
private Node next;

public Node (String data, Node next)
{
    this.data=data;
    this.next=next;
}

public String getData()
{
    return data;
}

public Node getNext()
{
    return next;
}

public void setData(String s)
{
    data=s;
}

public void setNext(Node n)
{
    next=n;
}

public String toString() {
    return "Node [data=" + data + ", next=" + next + "]";
}


public static void main(String[] args) {


//      Node cNode = new Node ("c",null);
//      Node bNode = new Node ("b",cNode);
//      Node list = new Node ("a",bNode);


        Node list = new Node ("A", new Node("B",new Node("C",null)));



            getThird(list);
            insertSecond(list,"k");
            size(list);




    }

    //1st method
    public static String getThird(Node list)
    {
        return list.getNext().getNext().getData();      
    }

    //2nd method
    public static void insertSecond (Node list, String s)
    {
        Node newNode=new Node("s",null);
        newNode.setNext(list.getNext());
        list.setNext(newNode);
    }

    //3rd method
    public static int size(Node list)
    {
        int count=0;
        while(list!=null)
        {
            count++;
            list=list.getNext();
        }
        return count;
    }

}

【问题讨论】:

  • 认为应该发生什么?
  • 添加断点并观察变量
  • 你没有打印任何东西。这将是导致没有输出的主要原因。
  • 我想你忘记了实际的输出...使用System.out.print或打印行来显示输出。
  • 打印返回值。

标签: java eclipse compiler-construction


【解决方案1】:

如果您希望将其打印到您的控制台,则需要将其打印出来。

正如 Stackexchange 上已经发布的那样...这里是printing nodes from a singly-linked list

你需要这样做:

System.out.print(list.toString());

【讨论】:

  • 这不需要是评论。它回答了这个问题。但是,为了将来参考,如果您不能发表评论,您不应该试图找到解决方法。你不应该发表评论。
  • 好的,谢谢(已解决)。我只是认为方法中的“返回”会打印在屏幕上
  • 现在我可以发表评论了,所以我现在不需要使用解决方法:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多