Java 二次公式代码错误(与主线和1线有关)
我看到了一些其他的二次公式问题,但似乎没有一个遇到这个问题 本质上有两个问题,一个是通过插入“}”解决的代码块会产生更多问题,另一个是它不会运行。我对java有点陌生,这是我第一个真正复杂的代码集,但我希望我只是犯了一些现在看不到的小错误Java 二次公式代码错误(与主线和1线有关),java,eclipse,Java,Eclipse,我看到了一些其他的二次公式问题,但似乎没有一个遇到这个问题 本质上有两个问题,一个是通过插入“}”解决的代码块会产生更多问题,另一个是它不会运行。我对java有点陌生,这是我第一个真正复杂的代码集,但我希望我只是犯了一些现在看不到的小错误 package package10; public class QuadraticFormula { public static void main(String[] args) { }//Brought in the
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);
}
}
package10;
公共类二次公式
{
公共静态void main(字符串[]args)
{
}//当Eclipse不断坚持这是必要的时,它带来了静态的空白
//文件名是QuadraticFormulaMain.java,并且在同一个目录中。我是否应该编写此文件来引用它?
公共静态字符串求积公式(int A、int B、int C)
{
}
INTA=1;
int B=-7;
int C=10;
//将整数设置为易于测试的数字
{
System.out.println(“Terry Peters”);//那是我的名字
对象查找解决方案;
System.out.println(findSolutions);
}
//运行并打印出下面找到的解决方案
专用静态整数鉴别器(整数A、整数B、整数C)
{
//用适当的方程式设置判别式
返回(B*B)-(4*A*C);
}
私有静态intnumberofsolutions(inta、intb、intc)
{
//实际解的数目,检查判别式是正是负还是零
if(判别式(A,B,C)>0)
返回2;
if(判别式(A,B,C)==0)
返回1;
if(判别式(A,B,C)<0)
返回0;
}
//这将执行开头提到的查找解决方案字符串
//不管出于什么原因,所有的返回语句都显示错误,但表示Eclipse有一个快速修复方法。
//这是一个谎言,因为它所说的一切都是“没有可用的建议”
私有静态字符串查找解决方案(int A、int B、int C)
{
如果(溶液数(A,B,C)==0);
System.out.println(“没有解决方案”);
返回“没有解决方案”;
if(解的个数(A,B,C)==1)
return(“有一个解决方案,它是”+plusSolution(A,B,C));
if(解的个数(A,B,C)==2)
return“有两种解决方案,它们是”+plusSolution(A,B,C)+”和“+minusSolution(A,B,C);
专用静态双加解决方案(int A、int B、int C)
{
返回((B*-1)+数学sqrt((B*B)-(4*A*C))/(2*A);
}
专用静态双精度解决方案(int A、int B、int C)
{
返回((B*-1)-Math.sqrt((B*B)-(4*A*C))/(2*A);
}
}
试试这个
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);
}
}
- 我所做的是制作一个有效的构造函数
公共平方公式(…)
- 当您将值传递给构造函数时。它将是类字段的值
- 对于
,您不需要参数,只需使用类字段即可findSolutions()
- 此外,我还将一些
语句更改为if
,这样您就不会收到该方法需要返回值的错误else if
There are two solutions and they are -0.5857864376269049and-3.414213562373095
这里有几个问题:
- 构造函数
不应返回任何内容QuadraticFormula()
- 所有语句都需要与一个方法关联(检查花括号,确保所有与方法关联的内容都在其中;如果方法中没有任何内容,则需要将其放入构造函数中,或者为其创建一个方法)
- 普通类中的所有方法都需要以大括号结束,而不是分号。以分号结束的方法可以在中找到,而不是普通类
- 如果您的代码应该从名为
的类执行,那么只有该类需要QuadraticFormulaMain.java
方法main()
- 如果要将字符串打印到终端屏幕,应使用
。如果要将该信息发送到System.out.println(sampleString)
,则应QuadraticFormulaMain
。不必同时执行这两项操作返回sampleString
- 您需要在
的QuadraticFormulaMain
方法中实例化main()
。您不需要在QuadraticFormulaMain
中引用QuadraticFormulaMain
QuadraticFormulaMain
我没有为您编写正确的代码,因为看起来您需要练习。请尝试通过查看这些建议来修复代码,看看是否可以使其正常工作。与使用其他人的版本相比,您将学到更多。我甚至不确定从何处开始。啊,我明白了,谢谢您的支持。java语法新手这个练习有点帮助。你有什么好的链接可以链接到一些项目示例,这是一个很好的练习吗?@user3234240哈哈,没问题。我不久前就在那里。有一些非常好的练习问题和即时反馈。这可能是你最好只研究语法。如果你还在尝试理解对象的话语言非常好。他们有很多关于更具体概念或软件包的教程,你可能想了解。大约一年前我在那里学习了很多swing的基础知识。这显示了大量的错误和我自己的一些过度复杂(也就是说,如果鉴别器不是正数或零,那么假设它是负数要容易得多,然后再创建另一个语句)虽然我注意到您将新公式从1-7和10更改为2-8和4。不确定原因,但将其插入原始python代码后,结果是相同的,因此它也可以处理多个数字。
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);
}
}