【问题标题】:Create a java program to solve quadratic equations创建一个求解二次方程的java程序
【发布时间】:2013-06-02 23:42:57
【问题描述】:

求解一个二次方程

到目前为止,我已经写下了以下内容。不知道怎么介绍第二种方法

public static void main(string args[]){

}

public static  double quadraticEquationRoot1(int a, int b, int c) (){

}

if(Math.sqrt(Math.pow(b, 2) - 4*a*c) == 0)
{
    return -b/(2*a);
} else {
    int root1, root2;
    root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    return Math.max(root1, root2);  
}

}

【问题讨论】:

标签: java applet computer-science


【解决方案1】:

首先,您的代码无法编译——在public static double quadraticEquationRoot1(int a, int b, int c) () 的开头之后,您有一个额外的}。

其次,您不是在寻找正确的输入类型。如果您需要double 类型的输入,请确保正确声明该方法。当它们可能是双精度时(例如,root1 和 root2),也要小心声明为 int。

第三,我不知道你为什么有 if/else 块——最好直接跳过它,只使用当前在 else 部分中的代码。

最后,解决您最初的问题:只需创建一个单独的方法并使用Math.min() 而不是Math.max()。

所以,回顾一下代码:

public static void main(string args[]){

}

//Note that the inputs are now declared as doubles.
public static  double quadraticEquationRoot1(double a, double b, double c) (){    
    double root1, root2; //This is now a double, too.
    root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4*a*c)) / (2*a);
    return Math.max(root1, root2);  
}

public static double quadraticEquationRoot2(double a, double b, double c) (){    
    //Basically the same as the other method, but use Math.min() instead!
}

【讨论】:

    【解决方案2】:

    那么您为什么不尝试使用完全相同的算法,然后在您的 return 语句中使用 Math.min?

    另外,请注意,您没有在第一个 if 语句中考虑 $b$ 是否为负数。换句话说,您只需返回 $-b / 2a$ 但您不检查 $b$ 是否为负数,如果不是,那么这实际上是两个根中较小的一个而不是较大的。 对于那个很抱歉!我误解了发生了什么xD

    【讨论】:

    • @Maesumi 我的想法完全不正确,我非常抱歉!第一条语句有帮助吗?
    • 是的,只使用 Math.min 是有意义的。我不确定 OP 在问什么。
    • @Maesumi oop 没有意识到你不是 OP,呵呵
    【解决方案3】:
    package QuadraticEquation;
    
    import javax.swing.*;
    
    public class QuadraticEquation {
       public static void main(String[] args) {
          String a = JOptionPane.showInputDialog(" Enter operand a :  ");
          double aa = Double.parseDouble(a);
          String b = JOptionPane.showInputDialog(" Enter operand b :  ");
          double bb = Double.parseDouble(b);
          String c = JOptionPane.showInputDialog(" Enter operand c :  ");
          double cc = Double.parseDouble(c);
          double temp = Math.sqrt(bb * bb - 4 * aa * cc);
          double r1 = ( -bb + temp) / (2*aa);
          double r2 = ( -bb -temp) / (2*aa);
          if (temp > 0) {
                JOptionPane.showMessageDialog(null, "the equation has two real roots" +"\n"+" the roots are : "+  r1+" and " +r2);
    
            }
          else if(temp ==0) {
                JOptionPane.showMessageDialog(null, "the equation has one root"+  "\n"+ " The root is :  " +(-bb /2 * aa));
    
            }
    
          else{
    
                JOptionPane.showMessageDialog(null, "the equation has no real roots !!!");
            }
        }        
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多