我有一个用于解决二次方程式的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);
}
}
}
答案 0 :(得分:0)
您的方法需要在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);
}
}