我正在完成一项学校作业.我应该实现一个类并提供方法getSolution1和getSolution2.但是,我的代码有2个问题,我无法弄清楚.
问题#1在这一行:
solution1= ((-1*b)/> + Math.sqrt(Math.pow(b,2)-(4*a*c)));
Run Code Online (Sandbox Code Playgroud)
编译器告诉我:令牌">"上的语法错误,删除此令牌.我无法弄清楚我的语法是否有问题.
问题#2在产品线上:
String quadEquation= "The quadratic equation is "+ a + Math.pow(("x"),2) + " + " + b+"x"+ " + " + c+ " =0";
Run Code Online (Sandbox Code Playgroud)
在Math.pow下我得到一个错误,上面写着:Method pow不适用于参数String
这是我的整个代码:
public class QuadraticEquation
{
double a;
double b;
double c;
double solution1;
double solution2;
QuadraticEquation (double a, double b, double c){
a= this.a;
b= this.b;
c= this.c;
}
public boolean hasSolution (){
if ((Math.pow(b,2))- (4*a*c)<0){
return false;
}
else
{
return true;
}
}
public double getSolution1 (double a, double b, double c)
{
if (hasSolution){
solution1= ((-1*b) + Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;
return solution1;
}
}
public double getSolution2 (double a, double b, double c){
if (hasSolution){
solution1= ((-1*b) - Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;
return solution2;
}
}
public String toString (double a, double b, double c){
String quadEquation= "The quadratic equation is "+ a + "x^2" + " + " + b+"x"+ " + " + c+ " =0";
return quadEquation;
}
}
Run Code Online (Sandbox Code Playgroud)
由于这是一项学校作业,我正在寻找解决这个问题的指导.
谢谢.
您的第一个问题是您不能一起使用/>.这不是一个正确的操作. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
第二个问题是因为Math.pow需要两个数字.你有一个字符串.这就像试图获得苹果这个词的力量.你不能这样做.您必须先将该字符串转换为int.如何在Java中将String转换为int?
| 归档时间: |
|
| 查看次数: |
672 次 |
| 最近记录: |