我不断收到同样的错误,这对我来说毫无意义。
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");
}
}
答案 0 :(得分:1)
您的课程与文件名不具有相同的名称。 声明公共类时,文件和类名必须相同(不包括.java)
答案 1 :(得分:1)
错误为您提供了答案。因为您的文件名不同。
Exercise_03_01类是公共的,应在名为Exercise_03_01.java的文件中声明
文件名和Public类的名称必须相同。
请将文件名更改为exercise_03_01.java,它将解决此错误。