【发布时间】: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