Solving quadratic equation USING METHODS, java

你。 提交于 2020-01-07 07:37:08

问题


I have a java program written for solving the quadratic equation but the assignment requires me to have methods for each of the tasks: displaying the equation, determining if the equation has real solutions, calculating a solution, and displaying the solutions if they exist. I have methods for everything except for checking if the solutions are real except my methods are saying that they are undefined. Can someone please help me format my methods, I don't quite know how to implement them? here is the code I have thus far:

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

}


回答1:


Your methods need to be defined outside your main method. You can then call them in your main method like this:

displayEquation(a, b, c);

You also should remove the semicolons after your method definitions. It should look like this:

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


来源:https://stackoverflow.com/questions/26813045/solving-quadratic-equation-using-methods-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!