我看到了其他一些二次方程式问题,但似乎没有人遇到这个问题 本质上存在两个问题,一个是带有一个代码块,当通过插入“}”解决时会产生更多问题,另一个是不能运行的事实。我对java有点新,这是我第一个非常复杂的代码集,但我希望我只是犯了一些我现在看不到的小错误。
package package10;
public class QuadraticFormula
{
public static void main(String[] args)
{
}//Brought in the static void as Eclipse continually insisted this was neccesary. There is a main file I am supposed to be referencing
//The file name is QuadraticFormulaMain.java and is in the same directory. Is there a way I should be writing this to reference it?
public static String QuadraticFormula (int A, int B, int C)
{
}
int A = 1;
int B = -7;
int C = 10;
//Sets integers to easily testable numbers
{
System.out.println("Terry Peters");//Thats my Name
Object findSolutions;
System.out.println(findSolutions);
}
//Runs and Prints out the solution found below
private static int discriminant (int A, int B, int C)
{
//sets the discriminate with appropriate equation
return (B * B)-(4*A*C);
}
private static int numberOfSolutions (int A, int B, int C)
{
//The actual numerOfSolutions, checks if the discriminant is positive negative or zero
if (discriminant (A, B, C) > 0)
return 2;
if (discriminant (A, B, C) == 0 )
return 1;
if (discriminant (A, B, C) < 0)
return 0;
}
//This performs the find Solutions String mentioned in the beginning
//For whatever reason All return statements are showing errors, but says Eclipse has a quick fix.
//This is a lie as all it says is "No Suggestions Avalible"
private static String findSolutions (int A, int B, int C)
{
if (numberOfSolutions (A, B, C) == 0);
System.out.println ("There are no solutions");
return "There are no solutions";
if (numberOfSolutions (A, B, C) == 1)
return ("There is one solution and it is" + plusSolution (A, B, C));
if (numberOfSolutions (A, B, C) == 2)
return "There are two solutions and they are " + plusSolution (A, B, C) + "and" + minusSolution (A, B, C);
private static double plusSolution(int A, int B, int C)
{
return ((B * -1) + Math.sqrt((B * B)-(4*A*C))) / (2 * A);
}
private static double minusSolution (int A, int B, int C)
{
return ((B * -1) - Math.sqrt((B * B)-(4*A*C))) / (2* A);
}
}
答案 0 :(得分:1)
这里有几个问题:
QuadraticFormula()
不应返回任何内容QuadraticFormulaMain.java
的类执行,那么只有该类需要main()
方法。System.out.println(sampleString)
。如果您要将该信息发送至QuadraticFormulaMain
,则应return sampleString
。你不应该同时做这两件事。QuadraticFormula
的{{1}}方法中实例化QuadraticFormulaMain
。您无需在main()
中引用QuadraticFormulaMain
。我没有为您编写正确的代码,因为它看起来像您需要练习。尝试修改代码只需查看这些建议,看看是否可以让它工作。与仅使用其他人的版本相比,您将学到很多东西。
答案 1 :(得分:1)
You Have made several misktakes here is running snippet
of your code.
public class QuadraticFormula
{
public static void main(String[] args)
{
int A = 1;
int B = -7;
int C = 10;
System.out.println("Terry Peters");//Thats my Name
System.out.println(findSolutions(A,B,C));
}
private static int discriminant (int A, int B, int C)
{
//sets the discriminate with appropriate equation
return (B * B)-(4*A*C);
}
private static int numberOfSolutions (int A, int B, int C)
{
//The actual numerOfSolutions, checks if the discriminant is positive //negative or zero
if (discriminant (A, B, C) > 0)
return 2;
if (discriminant (A, B, C) == 0 )
return 1;
else
return 0;
}
//This performs the find Solutions String mentioned in the beginning
//For whatever reason All return statements are showing errors, but says Eclipse has a //quick fix.
//This is a lie as all it says is "No Suggestions Avalible"
private static String findSolutions (int A, int B, int C)
{
if (numberOfSolutions (A, B, C) == 0)
{
System.out.println ("There are no solutions");
return "There are no solutions";
}
else if (numberOfSolutions (A, B, C) == 1)
{
return ("There is one solution and it is" + plusSolution (A, B, C));
}
else
return "There are two solutions and they are " + plusSolution (A, B, C) + "and" + minusSolution (A, B, C);
}
private static double plusSolution(int A, int B, int C)
{
return ((B * -1) + Math.sqrt((B * B)-(4*A*C))) / (2 * A);
}
private static double minusSolution (int A, int B, int C)
{
return ((B * -1) - Math.sqrt((B * B)-(4*A*C))) / (2* A);
}
}
答案 2 :(得分:0)
试试这个
public class QuadraticFormula {
int A = 1;
int B = -7;
int C = 10;
public static void main(String[] args) {
QuadraticFormula formula = new QuadraticFormula(2, 8, 4);
System.out.println(formula.findSolutions());
}//Brought in the static void as Eclipse continually insisted this was neccesary. There is a main file I am supposed to be referencing
//The file name is QuadraticFormulaMain.java and is in the same directory. Is there a way I should be writing this to reference it?
public QuadraticFormula(int A, int B, int C) {
this.A = A;
this.B = B;
this.C = C;
}
//Sets integers to easily testable numbers
//Runs and Prints out the solution found below
private static int discriminant(int A, int B, int C) {
//sets the discriminate with appropriate equation
return (B * B) - (4 * A * C);
}
private int numberOfSolutions(int A, int B, int C) {
//The actual numerOfSolutions, checks if the discriminant is positive negative or zero
if (discriminant(A, B, C) > 0) {
return 2;
} else if (discriminant(A, B, C) == 0) {
return 1;
} else {
return 0;
}
}
//This performs the find Solutions String mentioned in the beginning
//For whatever reason All return statements are showing errors, but says Eclipse has a quick fix.
//This is a lie as all it says is "No Suggestions Avalible"
private String findSolutions() {
if (numberOfSolutions(A, B, C) == 1) {
return ("There is one solution and it is" + plusSolution(A, B, C));
} else if (numberOfSolutions(A, B, C) == 2) {
return "There are two solutions and they are " + plusSolution(A, B, C) + "and" + minusSolution(A, B, C);
} else {
System.out.println("There are no solutions");
return "There are no solutions";
}
}
private static double plusSolution(int A, int B, int C) {
return ((B * -1) + Math.sqrt((B * B) - (4 * A * C))) / (2 * A);
}
private static double minusSolution(int A, int B, int C) {
return ((B * -1) - Math.sqrt((B * B) - (4 * A * C))) / (2 * A);
}
}
public QuadraticFormula(...)
findSolutions()
,您不需要参数,只需使用类字段if
语句更改为else if
,这样您就不会因为该方法需要返回值而收到错误。输出
There are two solutions and they are -0.5857864376269049and-3.414213562373095