2

これは、多項式の項をフォーマットして String に追加するための toString メソッドです。その最終的な出力は、「+ 2x^2 + 2x + 1」のようなものです。最初のプラス記号を削除するにはどうすればよいですか? ありがとう

//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
       }
4

2 に答える 2