英文:
Syntax error on token "eqn" delete this token
问题
以下是翻译好的代码部分:
import java.util.*;
public class SolveEqu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double arr[] = new double[3]; // 作为参数传递的数组
System.out.println("Enter the three coefficients of the equation separated by spaces");
for (int i = 0; i < 3; i++)
arr[i] = sc.nextDouble();
double result[] = solveQuadratic(arr); // 未知
// 如果结果为负
if (result.length == 1) {
System.out.println("The equation has no real roots");
}
// 如果结果等于零
else if (result.length == 2) {
System.out.println(" The equation has just one real root");
System.out.format("The root is %.2f", result[1]);
}
// 如果结果为正
else {
System.out.println("The equation has two real roots");
System.out.format("The root are %.2f and %.2f", result[1], result[2]);
}
}
public static double[] solveQuadratic(double[] eqn) { // 从作业中提取的方法头
double discriminant = eqn[1] * eqn[1] - (4 * eqn[0] * eqn[2]); // 二次方程的判别式
if (discriminant > 0) { // 判别式为正,有两个实根
double r1 = (-eqn[1] + Math.sqrt(discriminant)) / (2 * eqn[0]); // 平方根的方程
double r2 = (-eqn[1] - Math.sqrt(discriminant)) / (2 * eqn[0]); // 平方根的方程
double res[] = { 2, r1, r2 };
return res;
} else if (discriminant == 0) { // 判别式等于零,方程有一个实根
double r1 = (-eqn[1]) / (2 * eqn[0]);
double res[] = { 1, r1 };
return res;
} else { // 方程没有实根
double res[] = { 0 }; // 未知
return res; // 未知
}
}
}
关于你提到的代码中的错误部分:
double discriminant = eqn[1] * eqn[1] - (4 * eqn[0] * eqn[2]);
这一行代码出现了语法错误。它的目的是计算二次方程的判别式。错误是由于乘法操作符 *
前后的数字之间没有运算符连接。正确的写法应该是:
double discriminant = eqn[1] * eqn[1] - (4 * eqn[0] * eqn[2]);
英文:
I am fairly new to Java and taking a course but cannot figure out this small syntax error that I keep on getting. I have been researching and staring at the problem for a while now and just cannot figure it out. I am not asking for someone to complete the project just need help with this 1 error. Sorry if I am not posting in the right section. Below is the code....
import java.util.*;
public class SolveEqu {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double arr[]=new double[3]; //passing array as parameter
System.out.println("Enter the three coefficients of the equation separated by spaces");
for(int i=0;i<3;i++)
arr[i]=sc.nextDouble();
double result[]=solveQuadratic(arr); // unknown
//if result is negative
if(result.length==1) {
System.out.println("The equation has no real roots");
}
//if result is = to zero
else if(result.length==2) {
System.out.println(" The equation has just one real root");
System.out.format("The root is %.2f",result[1]);
}
//if result is positive
else
{
System.out.println("The equation has two real roots");
System.out.format("The root are %.2f and %.2f",result[1],result[2]);
}
}
public static double[] solveQuadratic(double[] eqn) { // method heading from assignment
double discriminant=eqn[1]eqn[1]-(4eqn[0]eqn[2]); // discriminant of the quadratic equation
if(discriminant>0) { // discriminant is positive and has 2 real roots
double r1=(-eqn[1]+Math.sqrt(discriminant))/(2eqn[0]); // equation for square roots
double r2=(-eqn[1]-Math.sqrt(discriminant))/(2eqn[0]); //equation for square roots
double res[]= {2,r1,r2};
return res;
}
else if(discriminant==0) //equal to zero the equation has just one real root
{
double r1=(-eqn[1])/(2eqn[0]);
double res[]= {1,r1};
return res;
}
else // the equation has no real roots
{
double res[]= {0}; //unknown
return res; //unknown
}
}
}
The line that is causing me trouble and throwing errors on is the following ...
double discriminant=eqn[1]eqn[1]-(4eqn[0]eqn[2]);
It says "Invalid float literal number" and "Syntax error on taken "eqn", delete this token"
Could someone help explain what this error means?
答案1
得分: 1
@f1sh的评论是同一问题的多个示例中的第一个。
正如指出的,对于eqn[1]*eqn[1]
,您需要一个乘法运算符。
对于4eqn[0]eqn[2]
,情况也是如此。如果我记得我的二次方程,您在那里尝试进行乘法运算,所以您需要4*eqn[0]*eqn[2]
。该行应为:
double discriminant = eqn[1] * eqn[1] - (4 * eqn[0] * eqn[2]);
类似地,在计算r1
和r2
时,2eqn[0]
应为2*eqn[0]
。这个修正在计算r1
的两个地方都需要。
因此,基本规则是,在Java中,您必须在两个符号之间指定运算符。与数学不同,将它们写在一起并不意味着乘法。
英文:
The comment by @f1sh was the first of multiple examples of the same problem.
As pointed out, you needed a multiplication operator for eqn[1]*eqn[1]
.
For 4eqn[0]eqn[2]
, the same applies. If I remember my quadratics, you are trying to multiply there, so you need 4*eqn[0]*eqn[2]
. The line should read:
double discriminant=eqn[1]*eqn[1]-(4*eqn[0]*eqn[2]);
Similarly, in your calculation for r1
and r2
, 2eqn[0]
should be 2*eqn[0]
. That fix is needed in both places you calculate r1
.
So, the basic rule is that, in java, you must specify the operator between two symbols. Unlike in math, writing them next to one another does not imply multiplication.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论