2013-11-26 4 views
2

다항식의 항을 형식화하고이를 문자열에 추가하는 toString 메소드입니다. 최종 출력물은 "+ 2x^2 + 2x + 1"과 같습니다. 첫 번째 더하기 기호를 제거하는 방법은 무엇입니까?다항식의 toString 메소드

output = output.substring(2); 

이 당신에게 끝까지 인덱스 2에서 모든 문자를 제공합니다 : 감사

//toString method for returning ReesePolynomials in polynomaial format(for ReeseClient) 
     public String toString() 
     { 
      String output = ""; 
      //the following are situations for formatting the output of each term and adding them to String output 
      for (int i = 0; i < TermLength; i++) // For the number of terms stated by the user earlier, values will be given for each ReeseTerm in the poly array 
      { 
       if (poly[i].getExpo() == 0 && poly[i].getCoeff() > 0)  
       { 
        output += " + " + poly[i].getCoeff(); 
       } 

       else if (poly[i].getExpo() == 0 && poly[i].getCoeff() < 0) 
       { 
        output += " " + poly[i].getCoeff(); 
       } 

       else if (poly[i].getCoeff() == 1 && (poly[i].getExpo() != 0 && poly[i].getExpo() != 1)) 
       { 
        output += " + x^" + poly[i].getExpo(); 
       } 

       else if (poly[i].getCoeff() == 1 && (poly[i].getExpo() == 1)) 
       { 
        output += " + x";  
       } 

       else if (poly[i].getExpo() == 1 && poly[i].getCoeff() > 0) 
       { 
        output += " + " + poly[i].getCoeff() + "x"; 
       } 

       else if (poly[i].getExpo() == 1 && poly[i].getCoeff() < 0) 
       { 
        output += " " + poly[i].getCoeff() + "x"; 
       } 

       else if (poly[i].getCoeff() < 0 && (poly[i].getExpo() > 1 || poly[i].getExpo() < -1)) 
       { 
        output += " " + poly[i].getCoeff() + "x^" + poly[i].getExpo(); 
       } 

       else if (poly[i].getCoeff() == 0) 
       {} 

       else 
       { 
        output += " + " + poly[i].getCoeff() + "x^" + poly[i].getExpo(); 
       } 

      } 

     return output;  // the final string output is returned to be printed when called upon in main 
     } 
+0


가 처음부터 거기에 넣지 마십시오보십시오. – EJP

+0

드문 경우를 제외하고는 사실 이후에 fencepost 문제를 수정하는 것보다 훨씬 어렵습니다 (예 : 재귀 적으로 목록을보고 콘솔에 인쇄하는 경우 등). –

+0

@ BigEndian 동의하지 않습니다. 실수를하지 않으면 실수 한 후에 수정하는 것이 훨씬 간단합니다. – EJP

답변

1

output = output.subString(2);
return output;

관련 문제