【问题标题】:Simplify the radical in the quadratic formula?简化二次公式中的根式?
【发布时间】:2016-02-28 14:01:36
【问题描述】:

我是 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。然后生成介于 a 和 A 之间的随机双精度值 numa、numb 和 numc。然后程序计算判别式, d,作为双。然后取d的平方根,即r。然后程序完成计算两个解 s 和 S。然后程序打印两个解,如果判别式不小于 0,并将它们截断到小数点后两位。

【问题讨论】:

  • 我不是数学(或 java)人,所以我会在错误的地方为您提供有关您最初问题的建议,但作为一名程序员,一般来说,我强烈建议您重新考虑你的变量命名策略。 2 个变量 (A, a) 持有不同的值且名称仅因大小写而异,这绝对是 WTF 类型反应的磁石,是长期可维护性的灾难(您是否应该编写更复杂的程序,这是需要考虑的)。我建议改为 (a, b) 或 (a1, a2)。
  • 有什么理由使用双精度而不是整数或长整数?浮点数更难处理,你的例子暗示你想用整数或分数打印你的解决方案。

标签: java quadratic


【解决方案1】:

基本算法非常简单:

  1. 在判别式中分解数字
  2. 从部首中取出出现两次的因子

这是一个例子:

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

请注意,如果判别式不是整数,这将不起作用。

【讨论】:

  • 另外,在取平方根之前一定要检查d &lt; 0
【解决方案2】:

假设我理解您的目标是以简化的方式打印判别式的平方根,这应该可以工作(我使用 ? 而不是我没有时间查找如何打印的 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));
    }

希望这会有所帮助!

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我个人认为这是用最少的代码得到正确答案的最简单方法:

      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 中打印答案。祝你好运。 我希望这会有所帮助。

      【讨论】:

        【解决方案5】:

        我基本上创建了一个函数来查找平方根的平方因子并将其用于其他所有内容。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-28
          • 1970-01-01
          • 2018-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-23
          相关资源
          最近更新 更多