【发布时间】:2021-01-07 20:15:36
【问题描述】:
我不断收到同样的错误,这对我来说毫无意义。
Exercise03_01.java:5: error: class Exercise_03_01 is public, should be declared in a file named Exercise_03_01.java
public class Exercise_03_01 {
这是我的代码:
import java.util.Scanner;
public class Exercise_03_01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b and c: ");
// Prompt for user to enter the inputs
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
// Compute the discriminant of the equation.
double discriminant = Math.pow(b, 2) - 4 * a * c;
System.out.print("The equation has ");
if (discriminant > 0)
{
double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);
double root2 = (-b - Math.pow(discriminant, 2)) / (2 * a);
System.out.println("two roots " + root1 + " and " + root2);
}
else if (discriminant == 0)
{
double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);
System.out.println("one root " + root1);
}
else
System.out.println("no real roots");
}
}
【问题讨论】:
-
那么,代码是在一个名为exercise_03_01.java 的文件中吗?
-
错误信息告诉你问题到底出在哪里。您的文件名需要与公共类的名称匹配。
Exercise03_01 != Exercise_03_01 -
他们所说的或者你执行的地方是错误的。转到命令提示符/终端中的文件位置,然后运行它(使用
cd file_directory去那里) -
@TheLegend42 我认为错误消息表明了这种情况下的问题。
标签: java