【问题标题】:Solving quadratic equation USING METHODS, java使用方法求解二次方程,java
【发布时间】:2014-11-08 02:23:08
【问题描述】:

我有一个为求解二次方程而编写的 Java 程序,但作业要求我为每项任务提供方法:显示方程、确定方程是否有实解、计算解以及显示解(如果有)存在。除了检查解决方案是否真实之外,我对所有事情都有方法,除了我的方法说它们是未定义的。有人可以帮我格式化我的方法吗,我不太知道如何实现它们?这是我到目前为止的代码:

import java.util.Scanner;
public class QuadraticFormula {

public static void main(String[] args)
{
    //Creating scanner and variables
    Scanner s = new Scanner(System.in);
    System.out.println("Insert value for a: ");
    double a = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for b: ");
    double b = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for c: ");
    double c = Double.parseDouble(s.nextLine());

    //Display format for negatives
    displayEquation(double a, double b, double c);{
    if (b > 0 && c > 0 ){
        System.out.println(a + "x^2 + " + b + "x + " + c + " =0");}
    if (b < 0 && c > 0 ){
        System.out.println(a + "x^2 " + b + "x + " + c + " =0");}
    if (b > 0 && c < 0 ){
        System.out.println(a + "x^2 + " + b + "x " + c + " =0");}
    if (b < 0 && c < 0 ){
        System.out.println(a + "x^2 " + b + "x " + c + " =0");}
    s.close();
    }
    //The work/formula
    private static double calculateSolution(double a, double b, double c); 
    {
    double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return answer1;
    }

    private static double calculateSolution(double a, double b, double c); 
    {
    double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    return answer2;
    }
    //Display results and check if the solution is imaginary (real or not)
    private static void displaySolutions(double answer1, double answer2); {
    if (Double.isNaN(answer1) || Double.isNaN(answer2))
    {
        System.out.println("Answer contains imaginary numbers");
    } else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}

}

【问题讨论】:

    标签: java html eclipse computer-science


    【解决方案1】:

    您的方法需要在您的主要方法之外定义。然后你可以像这样在你的 main 方法中调用它们:

    displayEquation(a, b, c);
    

    您还应该删除方法定义后的分号。它应该是这样的:

    public class QuadraticFormula {
    
    public static void main(String[] args)
    {
        //Creating scanner and variables
        Scanner s = new Scanner(System.in);
        System.out.println("Insert value for a: ");
        double a = Double.parseDouble(s.nextLine());
        System.out.println("Insert value for b: ");
        double b = Double.parseDouble(s.nextLine());
        System.out.println("Insert value for c: ");
        double c = Double.parseDouble(s.nextLine());
        s.close();
    }
        //Display format for negatives
        public static void displayEquation(double a, double b, double c) {
            if (b > 0 && c > 0 ){
                System.out.println(a + "x^2 + " + b + "x + " + c + " =0");}
            if (b < 0 && c > 0 ){
                System.out.println(a + "x^2 " + b + "x + " + c + " =0");}
            if (b > 0 && c < 0 ){
                System.out.println(a + "x^2 + " + b + "x " + c + " =0");}
            if (b < 0 && c < 0 ){
                System.out.println(a + "x^2 " + b + "x " + c + " =0");}
        }
    
        private static double calculateSolution(double a, double b, double c) {
            double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
            return answer2;
        }
    
        //Display results and check if the solution is imaginary (real or not)
        private static void displaySolutions(double answer1, double answer2) {
            if (Double.isNaN(answer1) || Double.isNaN(answer2))
            {
                System.out.println("Answer contains imaginary numbers");
            }   
            else System.out.println("The values are: " + answer1 + ", " + answer2);
        }
    }
    

    【讨论】:

    • 老实说我试过了,但它仍然不正确。 @dogwin
    猜你喜欢
    • 2013-03-02
    • 2022-06-11
    • 1970-01-01
    • 2011-05-18
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多