1

私はJavaにかなり慣れていないので、二次式を単純化するコードを書くのに助けが必要です. 現在、私のプログラムは 2 つの解を小数点以下 2 桁に切り捨てています。しかし、判別式の二乗を単純化する方法がわかりません。たとえば、判別式が 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 を入力させています。次に、a と A の間のランダムな double 値 numa、numb、および numc が生成されます。次に、プログラムは判別式 d を次のように計算します。ダブル。次に、d の平方根を取ります。これが r です。次に、プログラムは 2 つの解 s と S の計算を終了します。判別式が 0 以上の場合、プログラムは 2 つの解を出力し、小数点以下 2 桁まで切り捨てます。

4

5 に答える 5

4

基本的なアルゴリズムは非常に単純です。

  1. 判別式の数を因数分解する
  2. 根号のうち二度現れる因数を取る

次に例を示します。

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

判別式が非整数の場合、これは機能しないことに注意してください。

于 2012-03-07T03:32:29.000 に答える
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);




    }

}
于 2013-03-21T14:54:09.847 に答える
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));
    }

お役に立てれば!

于 2012-03-07T03:42:55.023 に答える
0

個人的には、これが最小限のコードで正しい答えを得るための最も簡単な方法だと思います。

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).
    }
}

ただし、spuareroots で回答を出力する方法はわかりません。頑張ってください。これが役立つことを願っています。

于 2013-11-28T18:43:40.660 に答える
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);
        }



    }
于 2016-02-28T06:01:36.367 に答える