2015-09-25 21 views
4

我有這個解決二次方程的程序,但每當我嘗試在給定係數後輸出方程時,我需要以ax^2 +/- bx +/c的形式打印。我怎樣才能讓我的方程打印出數字的符號作爲「+/-」?給出其係數打印出二次方程。

package quadraticsolver; 
    import java.util.Scanner; 
    /** 
    * 
    * @author Trevor 
    */ 
    public class QuadraticSolver 
    { 
     public static void main(String[] args) 
     { 
      Scanner a = new Scanner(System.in); 
      Scanner b = new Scanner(System.in); 
      Scanner c = new Scanner(System.in); 
      float qCoeff, lCoeff, constant, discriminant, x1, x2, x3, x4; 

      System.out.print("Enter the coefficient of the quadratic term -> "); 
      qCoeff = a.nextFloat(); 
      System.out.print("Enter the coefficient of the linear term -> "); 
      lCoeff = b.nextFloat(); 
      System.out.print("Enter the constant term -> "); 
      constant = c.nextFloat(); 

      discriminant = (float) (Math.pow(lCoeff,2) - (4*qCoeff*constant)); 

      if (qCoeff == 0) 
      { 
       System.out.println("The equation must have a non-zero quadratic term."); 
      }    
      else if (discriminant == 0) 
      { 
       x1 = (float) (-1*lCoeff)/(2*qCoeff); 
       System.out.printf("Equation: %.5fx^2+%.5fx+ %.5f = 0.00000%n", qCoeff, lCoeff, constant); 
       System.out.printf("Solution: x={%.5f}", x1); 
      } 
      else if (discriminant > 0) 
      { 
       x1 = (float) ((-1*lCoeff+Math.sqrt(discriminant))/(2*qCoeff)); 
       x2 = (float) ((-1*lCoeff-Math.sqrt(discriminant))/(2*qCoeff)); 
       System.out.printf("Equation: %.5fx^2 + %.5fx + %.5f = 0.00000%n", qCoeff, lCoeff, constant); 
       System.out.printf("Solution: x={%.5f, %.5f%n}", x1, x2); 


      } 
      else if (discriminant <0) 
      { 
       x1 = (float) (-1*lCoeff)/(2*qCoeff); 
       x2 = (float) Math.abs(Math.sqrt((Math.abs(discriminant)))/(2*qCoeff)); 
       x3 = (float) (-1*lCoeff)/(2*qCoeff); 
       x4 = (float) Math.abs(Math.sqrt((Math.abs(discriminant))/(2*qCoeff))); 
       System.out.printf("Equation: %.5fx^2+%.5fx+%.5f = 0.00000%n", qCoeff, lCoeff, constant); 
       System.out.printf("Solution: x={%.5f+%.5fi, %.5f-%.5fi}", x1, x2, x3, x4); 

      } 
      else 
       System.out.println("Invalid type. Please input a number"); 



     } 

    } 

回答

1

可以包括的符號通過在格式說明符中包含+來格式化字符串中的數字。例如,

System.out.printf("Equation: %.5fx^2 %+.5fx %+.5f = 0.00000%n", 1.0, -2.0, 3.0); 

打印Equation: 1.00000x^2 -2.00000x +3.00000 = 0.00000

如果要適當的間隔(例如1.0 - 2.0 + 3.0),則可以替代地具有符號爲您設置基於所述數字的符號的獨立字符。

例子:

float qCoeff = 1.0f, lCoeff = -2.0f, constant = 3.0f; 
char lSign = lCoeff < 0 ? '-' : '+'; 
char constSign = constant < 0 ? '-' : '+'; 

System.out.printf("Equation: %.5fx^2 %c %.5fx %c %.5f = 0.00000%n", 
        qCoeff, lSign, Math.abs(lCoeff), constSign, Math.abs(constant)); 

打印Equation: 1.00000x^2 - 2.00000x + 3.00000 = 0.00000

Math.abs()電話有那麼它不打印如1.0 - -2.0

+0

咦事實證明,我使用的例子方程('x^2 - 2x + 3 = 0')沒有實值解。 – Sizik

2

如果你想打印出特殊字符像±

您可以通過文字像對列表進行迭代:

for(int x=0; x<256; x++) 
    System.out.println("Symbol of " + x + ": " + ((char)x)); 

在我身邊當我打印:

System.out.println(((char)177)); 

它給了我±

但是,我認爲這可能相當不安全,因爲在另一個工作站上運行時,屏幕上顯示的符號可能不一樣。

+1

這是177對我來說也是,有什麼是值得 –

1

使用此%+.5fSystem.out.printf打印數量

'+'標誌的符號用於數字值,以顯示其標誌

有點像

System.out.printf("Equation: %+.5fx^2 %+.5fx %+.5f = 0.00000%n", qCoeff, lCoeff, constant);