英文:
Can anyone help me understand what is wrong with this code from my Java class
问题
以下是您的代码的翻译部分:
我一直在得到同样不合理的错误。
```none
Exercise03_01.java:5: 错误: 类Exercise_03_01是public的,应在名为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("输入a、b和c:");
// 提示用户输入输入量
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
// 计算方程的判别式。
double discriminant = Math.pow(b, 2) - 4 * a * c;
System.out.print("该方程有");
if (discriminant > 0)
{
double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);
double root2 = (-b - Math.pow(discriminant, 2)) / (2 * a);
System.out.println("两个根" + root1 + "和" + root2);
}
else if (discriminant == 0)
{
double root1 = (-b + Math.pow(discriminant, 2)) / (2 * a);
System.out.println("一个根" + root1);
}
else
System.out.println("没有实根");
}
}
英文:
I keep getting the same error which makes no sense to me.
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 {
Heres my code:
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");
}
}
答案1
得分: 1
你的类名与文件名不同。
当声明一个公共类时,文件名和类名必须相同(不包括 .java 部分)。
英文:
Your class does not share the same name with the filename.
when a public class is declared the file and classname must be the same (exluding the .java)
答案2
得分: 1
错误给出了答案。因为您的文件名不同。
类Exercise_03_01是public的,应在名为Exercise_03_01.java的文件中声明。
文件名和公共类的名称必须相同。
请将文件名更改为Exercise_03_01.java,这应该可以解决这个错误。
英文:
The error gave you the answer. as your file name is different.
> class Exercise_03_01 is public, should be declared in a file named Exercise_03_01.java
File name and name of the Public class must be the same.
Please change the Filename to Exercise_03_01.java, It should fix this error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论