求解二次方程使用方法,java [英] Solving quadratic equation USING METHODS, java
问题描述
import java.util.Scanner;
public class QuadraticFormula {
public static void main(String [] args)
{
//创建扫描器和变量
扫描仪s =新扫描仪(System.in);
System.out.println(Insert value for a:);
double a = Double.parseDouble(s.nextLine());
System.out.println(插入值为b:);
double b = Double.parseDouble(s.nextLine());
System.out.println(插入值为c:);
double c = Double.parseDouble(s.nextLine());
//显示底片的格式
displayEquation(double a,double b,double c); {
if(b> 0&& c> 0) {
System.out.println(a +x ^ 2 ++ b +x ++ c += 0);}
if(b<&& 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<&& c< 0){
System.out.println(a +x ^ 2+ b +x+ c += 0);}
s.close();
}
//工作/公式
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;
}
//显示结果并检查解决方案是否为虚构(实际或不实际)
private static void displaySolutions(double answer1,double answer2); {
if(Double.isNaN(answer1)|| Double.isNaN(answer2))
{
System.out.println(Answer包含虚数);
} else System.out.println(的值是:+ answer1 +,+ answer2);
}
}
}
您的方法需要在主要方法之外定义。然后,您可以按照以下主要方法调用它们:
displayEquation(a,b,c);
您还应该在方法定义之后删除分号。它应该如下所示:
public class QuadraticFormula {
public static void main(String [] args)
{
//创建扫描仪和变量
扫描仪s =新扫描仪(System.in);
System.out.println(Insert value for a:);
double a = Double.parseDouble(s.nextLine());
System.out.println(插入值为b:);
double b = Double.parseDouble(s.nextLine());
System.out.println(插入值为c:);
double c = Double.parseDouble(s.nextLine());
s.close();
}
//显示负数的格式
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& ;& 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);}
如果(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;
}
//显示结果并检查解决方案是否为虚构(实或否)
private static void displaySolutions(double answer1,double answer2){
if (Double.isNaN(answer1)|| Double.isNaN(answer2))
{
System.out.println(Answer包含虚数);
}
else System.out.println(的值是:+ answer1 +,+ answer2);
}
}
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);
}
}
}
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);
}
}
这篇关于求解二次方程使用方法,java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!