简体   繁体   English

无法打印输出用Java添加两个数字

[英]Can't print out Add two numbers in Java

I can't return the value from the function AddTwoNumber to main. 我无法从函数AddTwoNumber返回值main。 I have already checked the result in the function and it is correct. 我已经检查了函数中的结果,它是正确的。 However, when I pass the value from AddTwoNumber into ListNode start , it doesn't print anything. 但是,当我将值从AddTwoNumberListNode start ,它不会显示任何内容。 I think the problem is happening here: 我认为问题在这里发生:

ListNode dummy = new ListNode(0);
ListNode node = dummy;

but I'm don't know how to solve it. 但我不知道如何解决。

ListNode.cs: ListNode.cs:

class ListNode {
    public int data;       // data stored in this node
    public ListNode next;  // link to next node in the list

    // post: constructs a node with data 0 and null link
    public ListNode() {
        this(0, null);
    }

    // post: constructs a node with given data and null link
    public ListNode(int data) {
        this(data, null);
    }

    // post: constructs a node with given data and given link
    public ListNode(int data, ListNode next) {
        this.data = data;
        this.next = next;
    }
}

Main.cs: Main.cs:

public static void main(String[] args) {

    ListNode first = new ListNode(8,
                     new ListNode(9,
                     new ListNode(7   )));

    ListNode second = new ListNode(3,
                      new ListNode(5,
                      new ListNode(6  )));

    ListNode start = AddTwoNumber(first,second);

    while (start!=null) {
        System.out.println(start.next);
        start=start.next;
    }

}

public static ListNode AddTwoNumber(ListNode first, ListNode second) {
    ListNode dummy = new ListNode(0);
    ListNode node = dummy;
    int Digitsten = 0;
    int sum = 0;
    //Once fit first&second =null & Digitsten=0,the code can stop
    while (first != null || second != null || Digitsten != 0) {

        if (first != null && second != null) {
            sum += first.data + second.data + Digitsten;
        } else if (first!= null) {
            sum += first.data + Digitsten;
        } else if (second!= null) {
            sum += second.data + Digitsten;
        } else { 
            sum=Digitsten; `enter code here`
        }

        int DigitsOne = sum % 10;

        Digitsten = sum / 10;

        node = new ListNode(DigitsOne);
        node = node.next;

        if (first == null) {
            first = null;
        } else {
            first = first.next;
        }

        if (second == null) {
            second = null;
        } else {
            second = second.next;
        }
        sum = 0;

    }
    return dummy.next; //return the value to dummy ListNode
}

The real problem is inside your AddTwoNumber method. 真正的问题在您的AddTwoNumber方法内部。 You can't make node pointing to a new node, you should make its next pointing to a new node, before you move to next. 您不能使节点指向新节点,在移至下一个节点之前,应使其下一个指向新节点。

public static ListNode AddTwoNumber(ListNode first, ListNode second){

ListNode dummy = new ListNode(0);
ListNode node = dummy;
int Digitsten = 0;
int sum = 0;

while (first != null || second != null || Digitsten != 0) 
{
    if (first != null && second != null) 
    {
        sum += first.data + second.data + Digitsten;
    } 
    else if (first!= null) 
    {
        sum += first.data + Digitsten;
    } 
    else if (second!= null) 
    {
        sum += second.data + Digitsten;
    }
    else
    { 
        sum=Digitsten; `enter code here`
    }

    int DigitsOne = sum % 10;
    Digitsten = sum / 10;

    // LOOK HERE!!!
    node.next = new ListNode(DigitsOne);
    node = node.next;


    if (first == null) 
    {
        first = null;
    } 
    else
        first = first.next;

    if (second == null) 
    {
        second = null;
    } 
    else 
    {
        second = second.next;
    }
    sum=0;
}
return dummy.next;//return the value to dummy ListNode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM