0
我有一個java程序來編寫解決二次方程,但分配要求我有每個任務的方法:顯示方程,確定方程是否有真正的解決方案,計算解決方案,並顯示解決方案(如果存在)。我除了檢查解決方案是否真實,除了我的方法說它們是未定義的外,其他方法都有。有人可以幫我格式化我的方法,我不知道如何實現它們?這裏是我迄今爲止代碼:解決二次方程使用方法,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);
}
}
}
我誠實地嘗試過這一點,但它仍然沒有正確的閱讀。 @dogwin – 2014-11-08 18:06:55
究竟發生了什麼?當我運行代碼時,它工作正常。 – dogwin 2014-11-08 22:14:44