Java-Programm zur Lösung der quadratischen Gleichung wird mit Methoden

Ich habe ein java Programm geschrieben für die Lösung der quadratischen Gleichung, aber die Zuordnung verlangt von mir, haben Methoden für die einzelnen Aufgaben: Darstellung der Gleichung bestimmen, wenn die Gleichung reelle Lösungen, die Berechnung der Lösung und die Darstellung der Lösungen, wenn Sie existieren. Ich glaube, ich habe noch nicht ganz gelernt, oder kam zu einem vollen Verständnis der Umsetzung von Methoden, hier ist der code, den ich bisher:

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
    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
    double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);

    //Display results and check if the solution is imaginary (real or not)
    if (Double.isNaN(answer1) || Double.isNaN(answer2))
    {
        System.out.println("Answer contains imaginary numbers");
    } else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}
Können Sie erklären, in 3 Sätzen, was Ihr code macht, wo jeder Satz wird ein paar Worte? Dies wird geben Sie den Ausgangspunkt Ihrer Methode brechen
Was ist Ihre eigentliche problem? Warum sind Menschen upvoting, wenn die OP nicht angegeben, ein bestimmtes problem? Alles, was ich sehe, sind Ihre Anforderungen.

InformationsquelleAutor Joe Eastman | 2014-11-05

Schreibe einen Kommentar