Java 只有 returns 0 当方程不可因式分解时
Java only returns 0 when equation is unfactorable
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
我有这段代码,我正在尝试获取方程式的因式。如果解不是整数,那么它 returns "No real solution"。如果因素相同,那么它 return 是唯一的因素。如果有两个因素,那么它应该 return 两个因素。它在只有 1 个因子时起作用(例如,当 a=1、b=2 和 c=1 时),但当方程不可因式分解时不起作用,并且当有两个解时,它只有 returns 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
顺便说一句,我被迫做某些事情,例如创建 "findDiscriminant",因为工作表迫使我这样做。它给了我主要的方法,我应该从那里弄清楚一切。
你的基本代码逻辑有问题。
equalRoots()
应该叫做 hasOnlyOneSolution
并且应该正好是
return this.d == 0
.
noSolution()
应该只是 return this.d < 0
.
请注意,您从不调用findDiscriminant
,这会导致d
保持为0。
这是您在纸上解决此问题时所做的基本数学区分。并且必须在代码中完成完全相同的逻辑。
最后说明:您疯狂地混淆了字段和局部变量。为什么 findDiscriminant
return 和设置 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
}
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
我有这段代码,我正在尝试获取方程式的因式。如果解不是整数,那么它 returns "No real solution"。如果因素相同,那么它 return 是唯一的因素。如果有两个因素,那么它应该 return 两个因素。它在只有 1 个因子时起作用(例如,当 a=1、b=2 和 c=1 时),但当方程不可因式分解时不起作用,并且当有两个解时,它只有 returns 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
顺便说一句,我被迫做某些事情,例如创建 "findDiscriminant",因为工作表迫使我这样做。它给了我主要的方法,我应该从那里弄清楚一切。
你的基本代码逻辑有问题。
equalRoots()
应该叫做 hasOnlyOneSolution
并且应该正好是
return this.d == 0
.
noSolution()
应该只是 return this.d < 0
.
请注意,您从不调用findDiscriminant
,这会导致d
保持为0。
这是您在纸上解决此问题时所做的基本数学区分。并且必须在代码中完成完全相同的逻辑。
最后说明:您疯狂地混淆了字段和局部变量。为什么 findDiscriminant
return 和设置 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
}