请大家看我的题目:
只在 IF中实现 去掉“-->”
代码如下:
- Java code
package mystudy.j2se.nodeLink;public class Node { private String name; private Node next; public Node(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Node getNext() { return next; } public void setNext(Node node) { this.next = node; } }
- Java code
package mystudy.j2se.nodeLink;public class Link { public static void main(String[] args) { Node root = new Node("根节点"); Node n1 = new Node("第一节车厢"); Node n2 = new Node("第二节车厢"); Node n3 = new Node("第三节车厢"); root.setNext(n1); n1.setNext(n2); n2.setNext(n3); print(root); } private static void print(Node node) { // TODO Auto-generated method stub if(node != null){ System.out.print(node.getName()+"\t"+"-->"+"\t");//在这里打印完最后一个的时候,有“-->”,输出结果是:根节点 --> 第一节车厢 --> 第二节车厢 --> 第三节车厢 -->,在第三节的后面这个“-->”不要,要求打印结果为:根节点 --> 第一节车厢 --> 第二节车厢 --> 第三节车厢,只能在IF中修改 } if(node.getNext()!=null){ print(node.getNext()); } }}
------解决方案--------------------
- Java code
public class Link { public static void main(String[] args) { Node root = new Node("根节点"); Node n1 = new Node("第一节车厢"); Node n2 = new Node("第二节车厢"); Node n3 = new Node("第三节车厢"); root.setNext(n1); n1.setNext(n2); n2.setNext(n3); print(root); } private static void print(Node node) { if(node != null){// System.out.print(node.getName()+"\t"+"-->"+"\t"); //这句中的制表符 和-->放在下面的if语句中 System.out.print(node.getName()); } if(node.getNext()!=null){ System.out.print("\t"+"-->"+"\t");// 放在这里 print(node.getNext()); } }}
------解决方案--------------------