在同一行打印 "node list" 中的所有内容
print all the content in a "node list" on the same line
我是初学者,目前正在 java 工作,我需要使用节点内的内容在一行中编写一个方程式,如 5x^3 + 2x^4 + ...。
这是我的 class 节点:
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();
}
}
我的class Termo(这是一个带有系数和指数的对象)
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;
}
我创建了这个方法来打印内容,但我希望它在一行中:
public void escrevePolinomio (Node lista){
if(lista != null){
System.out.println(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
lista=lista.getnext();
escrevePolinomio(lista);
}
}
使用System.out.print()
代替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);
}
}
我是初学者,目前正在 java 工作,我需要使用节点内的内容在一行中编写一个方程式,如 5x^3 + 2x^4 + ...。
这是我的 class 节点:
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();
}
}
我的class Termo(这是一个带有系数和指数的对象)
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;
}
我创建了这个方法来打印内容,但我希望它在一行中:
public void escrevePolinomio (Node lista){
if(lista != null){
System.out.println(lista.getElement().getCoef()+"x^"+lista.getElement().getExp());
lista=lista.getnext();
escrevePolinomio(lista);
}
}
使用System.out.print()
代替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);
}
}