Wydrukuj wszystkie treści w "liście węzłów" w jednym wierszu

0

Pytanie

Jestem nowy, i obecnie pracuję z java, muszę napisać w jednej linii równanie, na przykład, 5x^3 + 2x^4 + ... za pomocą zawartości wewnątrz węzłów.

Oto mój węzeł klasy:

public class Node {
    private Termo element;
    private Node next;
    
    public Node(){
        element = null;
        next= null;
    }
    
    public Node( Termo element, Node next){
        this.element= element;
        this.next= next;
    }
    
     public void setnext( Node next){
        this.next= next;
    }
    
    public Node getnext(){
        return proximo;
    } 
    
     public void setElement( Termo element){
        this.element= element;
    }
    
    public Termo getElement(){
        return element;
    }
    
    @Override
    public String toString(){
        return "Termo: " + getElement().getCoef() + "x^" +getElement().getExp();
    }
}

Moja klasa Termo (który stanowi obiekt z kursem i wskaźnikiem)

private double coef;
private int exp;

public Termo() {
    coef=0;
    exp=0;
}

public Termo(double coef, int exp) {
    this.coef = coef;
    this.exp = exp;
}

public double  getCoef() {
    return coef;
}

public void setCoef(int coef) {
    this.coef = coef;
}

public int getExp() {
    return exp;
}

public void setExp(int exp) {
    this.exp = exp;
}

public Object getObject(){
    Termo obj = new Termo(this.coef, this.exp);
    return obj;
}

@Override
public String toString() {
    return coef + " " + exp;
}

Stworzyłem ten metoda, aby wydrukować zawartość , ale chcę, aby był w jednej linii:

public void escrevePolinomio (Node lista){
        if(lista != null){
            System.out.println(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
            lista=lista.getnext();
            escrevePolinomio(lista);
        }
    }
java nodes printing
2021-11-23 15:10:34
1

Najlepsza odpowiedź

0

Skorzystaj z System.out.print() zamiast System.out.println():

public void escrevePolinomio (Node lista){
    if(lista != null){
        System.out.print(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
        lista=lista.getnext();
        escrevePolinomio(lista);
    }
}
2021-11-23 15:19:29

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................