import java.util.Scanner;
public class QuadraticEquation
{
private int a, b, c, d, s1, s2;
public QuadraticEquation()
{
a = 1;
b = 2;
c = 1;
d = 0;
s1 = -1;
s1 = -1;
}
public QuadraticEquation(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int findDiscriminant(int a, int b, int c)
{
this.d = (int)Math.pow(b,2) - (4*a*c);
return this.d;
}
public boolean equalRoots()
{
if (getSolution1() == getSolution2())
{
return true;
} else {
return false;
}
}
public boolean noSolution()
{
if (Math.sqrt(this.d) == 0)
{
return false;
} else {
return true;
}
}
public int getSolution1()
{
this.s1 = (int)(-b + Math.sqrt(this.d))/(2*a);
return this.s1;
}
public int getSolution2()
{
this.s2 = (int)(-b - Math.sqrt(this.d))/(2*a);
return this.s2;
}
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
for (int k = 1; k <= 3; k++)
{
System.out.print("Enter a, b, and c: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
QuadraticEquation q = new QuadraticEquation(a,b,c);
if (q.noSolution() == true)
{
System.out.println("No real solution");
} else if (q.equalRoots() == true) {
System.out.println("The only solution is: " + q.getSolution1());
} else {
System.out.println("The two real solutions are: ");
System.out.println(q.getSolution1());
System.out.println(q.getSolution2());
} //Else
} //For
} //Main
} //QuadraticEquations
我有这个代码,我试图得到一个等式的因子。如果解决方案不是整数,那么它将返回&#34;没有真正的解决方案&#34;。如果因子相同,则返回唯一因子。如果有两个因素,那么它应该返回两个因素。当只有1个因子时(例如当a = 1,b = 2和c = 1时),它起作用,但当方程不可行时,它不起作用,并且当它出现时,它就不起作用。两个解决方案,它只返回1.
这是当前不正确的输出:
----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 1 1
The only solution is: 0
Enter a, b, and c:
----jGRASP: process ended by user.
----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 -5 6
The only solution is: 2
Enter a, b, and c:
----jGRASP: process ended by user.
----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 2 1
The only solution is: -1
Enter a, b, and c:
----jGRASP: process ended by user.
----jGRASP exec: java QuadraticEquation
Enter a, b, and c: 1 -4 4
The only solution is: 2
Enter a, b, and c:
编辑:
感谢bcsb1001,我修改了我的代码,它确实有效。
import java.util.Scanner;
public class QuadraticEquation
{
private int a, b, c, d;
public QuadraticEquation(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
this.d = findDiscriminant(a, b, c);
}
public int findDiscriminant(int a, int b, int c)
{
return (int)Math.pow(b,2) - (4*a*c);
}
public boolean equalRoots()
{
return this.d == 0;
}
public boolean noSolution()
{
return this.d < 0;
}
public int getSolution1()
{
return (int)(-b + Math.sqrt(this.d))/(2*a);
}
public int getSolution2()
{
return (int)(-b - Math.sqrt(this.d))/(2*a);
}
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
for (int k = 1; k <= 3; k++)
{
System.out.print("Enter a, b, and c: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
QuadraticEquation q = new QuadraticEquation(a,b,c);
if (q.noSolution() == true)
{
System.out.println("No real solution");
} else if (q.equalRoots() == true) {
System.out.println("The only solution is: " + q.getSolution1());
} else {
System.out.println("The two real solutions are: ");
System.out.println(q.getSolution1());
System.out.println(q.getSolution2());
} //Else
} //For
} //Main
} //QuadraticEquations
是的,我被迫做了某些事情,比如创造&#34; findDiscriminant&#34;因为工作表强迫我。它给了我主要的方法,我应该从那里弄清楚一切。
答案 0 :(得分:2)
您的基本代码逻辑有问题。
equalRoots()
应该被称为hasOnlyOneSolution
,而且应该是正确的
return this.d == 0
。
noSolution()
应该只是return this.d < 0
。
请注意,您从不致电findDiscriminant
,导致d
保持为0。
这是在纸上解决此问题时所做的基本数学区别。并且必须在代码中完成相同的逻辑。
最后注意:你正在混淆字段和局部变量,比如疯狂。为什么findDiscriminant
会返回一些内容并设置d
。为什么它会通过a
,b
和c
,而不是简单地访问具有相同内容的同名字段?
public class QuadraticEquation {
private int a, b, d;
public QuadraticEquation(int a, int b, int c) {
this.a = a;
this.b = b;
this.d = b * b - (4 * a * c);
}
public boolean hasOnlyOneSolution() {
return d == 0;
}
public boolean noSolution() {
return d < 0;
}
public int getSolution1() {
return (int) (-b + Math.sqrt(this.d)) / (2 * a);
}
public int getSolution2() {
return (int) (-b - Math.sqrt(this.d)) / (2 * a);
}
// + your main method
}