谁会改正这个Java程序
SquareEquation.javapackagetom.jiafei;publicclassSquareEquation{doublea,b,c;doubleroot...
SquareEquation.java
package tom.jiafei;
public class SquareEquation
{
double a,b,c;
double root1,root2;
boolean boo;
public SquareEquation(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
public void getRoots()
{
if(boo)
{
System.out.println("是一元2次方程");
double disk=b*b-4*a*c;
if(disk>=0)
{
root1=(-b+Math.sqrt(disk))/(2*a);
root2=(-b-Math.sqrt(disk))/(2*a);
System.out.printf("方程的根:%f,%f\n",root1,root2);
}
else
{
System.out.printf("方程没有实根\n");
}
}
else
{
System.out.println("不是一元2次方程");
}
}
public void setCoefficient(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
}
SunRise.java
import tom.jiafei.*;
class SunRise
{
public static void main(String args[ ])
{
SquareEquation equation=new SquareEquation(4,5,1);
equation.getRoots();
equation.setCoefficient(-3,4,5);
equation.getRoots();
}
} 展开
package tom.jiafei;
public class SquareEquation
{
double a,b,c;
double root1,root2;
boolean boo;
public SquareEquation(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
public void getRoots()
{
if(boo)
{
System.out.println("是一元2次方程");
double disk=b*b-4*a*c;
if(disk>=0)
{
root1=(-b+Math.sqrt(disk))/(2*a);
root2=(-b-Math.sqrt(disk))/(2*a);
System.out.printf("方程的根:%f,%f\n",root1,root2);
}
else
{
System.out.printf("方程没有实根\n");
}
}
else
{
System.out.println("不是一元2次方程");
}
}
public void setCoefficient(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
if(a!=0)
{
boo=true;
}
else
{
boo=false;
}
}
}
SunRise.java
import tom.jiafei.*;
class SunRise
{
public static void main(String args[ ])
{
SquareEquation equation=new SquareEquation(4,5,1);
equation.getRoots();
equation.setCoefficient(-3,4,5);
equation.getRoots();
}
} 展开
展开全部
main()函数必须写在public类中 。
改为: class SquareEquation {
double a, b, c;
double root1, root2;
boolean boo;
public SquareEquation (double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
if (a != 0) {
boo = true;
} else {
boo = false;
}
}
public void getRoots() {
if (boo) {
System.out.println("是一元2次方程");
double disk = b * b - 4 * a * c;
if (disk >= 0) {
root1 = (-b + Math.sqrt(disk)) / (2 * a);
root2 = (-b - Math.sqrt(disk)) / (2 * a);
System.out.printf("方程的根:%f,%f\n", root1, root2);
} else {
System.out.printf("方程没有实根\n");
}
} else {
System.out.println("不是一元2次方程");
}
}
public void setCoefficient(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
if (a != 0) {
boo = true;
} else {
boo = false;
}
}
}
public class SunRise {
public static void main(String args[]) {
SquareEquation equation = new SquareEquation (4, 5, 1);
equation.getRoots();
equation.setCoefficient(-3, 4, 5);
equation.getRoots();
}
}
另外,你的文件名必须改为和public类名一样,也就是把SquareEquation.java改为SunRise.java
改为: class SquareEquation {
double a, b, c;
double root1, root2;
boolean boo;
public SquareEquation (double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
if (a != 0) {
boo = true;
} else {
boo = false;
}
}
public void getRoots() {
if (boo) {
System.out.println("是一元2次方程");
double disk = b * b - 4 * a * c;
if (disk >= 0) {
root1 = (-b + Math.sqrt(disk)) / (2 * a);
root2 = (-b - Math.sqrt(disk)) / (2 * a);
System.out.printf("方程的根:%f,%f\n", root1, root2);
} else {
System.out.printf("方程没有实根\n");
}
} else {
System.out.println("不是一元2次方程");
}
}
public void setCoefficient(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
if (a != 0) {
boo = true;
} else {
boo = false;
}
}
}
public class SunRise {
public static void main(String args[]) {
SquareEquation equation = new SquareEquation (4, 5, 1);
equation.getRoots();
equation.setCoefficient(-3, 4, 5);
equation.getRoots();
}
}
另外,你的文件名必须改为和public类名一样,也就是把SquareEquation.java改为SunRise.java
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询