有人能帮我理解我Java课堂里这段代码有什么问题吗?

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月22日 01:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63997280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定