2012-03-07 62 views
1

我對Java相當陌生,需要編寫簡化二次公式的代碼。現在我的程序將兩個解決方案截斷爲兩位小數。但我不知道如何簡化判別式的平方。例如,如果判別式爲8,那麼我希望程序輸出2√2。請給我提供執行此操作所需的代碼嗎?簡化二次公式中的根?

package quadraticprogram; 

//This imports the DecimalFormat class, Scanner class, and all other Java classes. 
import java.text.DecimalFormat; 
import java.util.Scanner; 
import java.util.*; 

public class QuadraticProgram { 

    public static void main(String[] args) { 
    int a, A; 

    Scanner scan = new Scanner (System.in); 
    System.out.println ("Use integer value, enter minimum value of a:"); 
    a = scan.nextInt(); 

    System.out.println ("Use integer value, enter maximum value of A:"); 
    A = scan.nextInt(); 
    Random generator = new Random(); 

    // Generate random integers in the range from a to A 
    // and assign them to numa, numb, and numc 
    double numa = generator.nextInt(A - a + 1) + a; 
    double numb = generator.nextInt(A - a + 1) + a; 
    double numc = generator.nextInt(A - a + 1) + a; 

    System.out.println ("numa" + numa); 
    System.out.println ("numb" + numb); 
    System.out.println ("numc" + numc); 

    // Define d as the discriminant and take its square root 
    double d; 
    d = ((numb*numb)-(4*numa*numc)); 
    double r = Math.sqrt(d); 

    // Calculate the two solutions 
    double s = ((-numb + r)/(2*numa)); 
    double S = ((-numb - r)/(2*numa)); 

    // Truncate the two solutions to two decimal places. 
    DecimalFormat fmt = new DecimalFormat ("0.##"); 

    // If the discriminant is negative there are no real solutions. 
    if (d<0) { 
     System.out.println("No Real Solutions"); 
    } else { 
    // Print both solutions if the discriminant is not negative 
     System.out.print(fmt.format(s)); 
     System.out.println("," + fmt.format(S)); 
    } 
    } 
} 

眼下程序具有用戶輸入的最小整數,a和的最大整數,A.然後隨機雙值,NUMA,麻木,並生成NUMC是一個與A之間然後程序將判別式d計算爲雙。然後取d的平方根即r。然後程序完成計算兩個解s和S.然後程序打印這兩個解,如果判別式不小於0,並將它們截斷爲小數點後兩位。

+7

我不是數學(或Java)的傢伙,所以我會在錯誤的地方爲您提供有關你原來的問題,但作爲一個程序員,一般來說,我強烈建議你重新考慮你的可變的命名策略。 2個變量(A,a)保持不同的值,並且僅根據情況名稱不同,這對於WTF類型反應來說是一個絕對的磁鐵,對於長期可維護性來說是一種災難(你應該寫出更復雜的程序,這是一個考慮因素)。我建議(a,b)或(a1,a2)。 – Chris 2012-03-07 03:24:40

+0

是否有任何理由使用雙打而不是整數或長整數?浮點數更難處理,你的例子意味着你想用整數或分數打印你的解決方案。 – 2012-03-07 03:25:46

回答

4

的基本算法很簡單:

  1. 因素的判別
  2. 數以顯示出激進

這裏的兩倍因素是一個例子:

sqrt(180) = sqrt(2*2*3*3*5) = 2*3*sqrt(5) = 6*sqrt(5)

請注意,此wi如果判別式不是整數,則不起作用。

+0

另外,在取其平方根之前,一定要檢查'd <0'。 – 2012-03-07 03:34:18

0

假設我明白你的目標是以簡化方式打印判別者的平方根,這應該工作(我用?而不是sqrt符號,我沒有時間查找如何打印出來) :

 while(d%Math.pow(f, 2)!=0&&f>1){ 
     f--; 
    } 

    if(f>1&&d/Math.pow(f, 2)!=1){ 
     System.out.println(f+"?"+d/Math.pow(f, 2)); 
    }else{ 
     System.out.println(Math.sqrt(d)); 
    } 

希望這會有所幫助!

0

1.不確定你做了什麼,但這裏是我的解決方案。

import java.util.Scanner; 
public class quadform { 

    public static void main(String[] args) { 
    double a,b,c; 

    Scanner takea = new Scanner(System.in); 
    System.out.println("Enter variable a"); 
    double inputa = takea.nextDouble(); 

    Scanner takeb = new Scanner(System.in); 
    System.out.println("Enter variable b"); 
    double inputb = takeb.nextDouble(); 

    Scanner takec = new Scanner(System.in); 
    System.out.println("Enter variable c"); 
    double inputc = takec.nextDouble(); 


    a = inputa; 
    b = inputb; 
    c = inputc; 


    double rootone,roottwo; 
    double discriminant; 
    double thefirstpart,thesecondpart; 
    thefirstpart = Math.pow(b,2); 
    thesecondpart =4 *a *c; 
    discriminant = Math.sqrt(thefirstpart - thesecondpart); 
    rootone = (-(b)+ discriminant)/(2 *(a)); 
    roottwo = (-(b)- discriminant)/(2 *(a)); 

    System.out.println("The first root (+) is: " + rootone); 
    System.out.println("The second root(-) is: " + roottwo); 




    } 

} 
0

我個人認爲這將有最少的代碼量的正確答案最簡單的方法:我不知道該怎麼雖然打印spuareroots答案

import javax.swing.JOptionPane; 
public class ShortABC 
{ 
    public static void main(String[] args) 
    { 
    float a = Float.parseFloat(JOptionPane.showInputDialog(null, "Please, give in variable a", "Input variable A", JOptionPane.QUESTION_MESSAGE)), b = Float.parseFloat(JOptionPane.showInputDialog(null, "Please, give in variable b", "Input b", JOptionPane.QUESTION_MESSAGE)), c = Float.parseFloat(JOptionPane.showInputDialog(null, "Please, give in variable c", "Input c", JOptionPane.QUESTION_MESSAGE)), D = (float) (Math.pow(b, 2) - (4 * a * c)), x1 = (float) ((-b - Math.sqrt(D)/(2*a))), x2 = (float) ((-b + Math.sqrt(D)/(2*a)));//Input for all variables. 
    if(D < 0) JOptionPane.showMessageDialog(null,"No answers possible");//Output for answer(s). else if(D == 0) 
    else if(D == 0) JOptionPane.showMessageDialog(null,"One possible answer: " + x1);//Output for answer(s). 
    else if(D > 0) JOptionPane.showMessageDialog(null, "Two possible answers: " + x1 + " en " + x2);//Output for answer(s). 
    } 
} 

。祝你好運。 我希望這可以幫助。

0

我基本上創建了一個函數來查找平方根的平方因子並將其用於其他任何方面。

public static double sqFactor(double num) { 
    double sqInt = 1; 
    double lastInt=1; 
    for (int i=2; i<=num;) { 
     if (num%i==0) { 
      num /= i; 


      if (i==lastInt) { 
       sqInt *= i; 
       lastInt=1; 
       i=1; 
      } 
      else lastInt=i; 
      i=1; 

     } 

     i++; 

    } 
    return sqInt; 
} 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); 

     System.out.println("a ="); 
     double a = scan.nextDouble(); 

     System.out.println("b ="); 
     double b = scan.nextDouble(); 

     System.out.println("c ="); 
     double c = scan.nextDouble(); 

     double d = Math.pow(b, 2) -(4 * a * c); 
     if (d<0) { 
     d *= -1; 
     System.out.println("Your roots are " + -b +" +- " + sqFactor(d) +"i sqrt" + d/sqFactor(d)); 
     } 
     else { 
     double root1 = (-b + Math.sqrt(d))/(2 * a); 
     double root2 = (-b - Math.sqrt(d))/(2 * a); 

     System.out.println("Your first root value is " + root1); 
     System.out.println("Your second root value is " + root2); 
     } 



    }