【发布时间】:2016-03-07 15:13:04
【问题描述】:
我是一个对编程非常陌生的学生,并且已经用java编写了一个简单的程序来转换单位。
我在 Mac 上使用 ideone 在线编译器。我收到此错误
Main.java:7: error: class AssessmentOne is public, should be declared in a file named AssessmentOne.java
public class AssessmentOne{
^
1 error
这是我的代码(这是我正在处理的任务,因此得名):
package assessmentOne.pkg1;
import java.util.Scanner;
// MY NAME Assessment One
public class AssessmentOne{
public static void main(String[] args)
{ int choice;
do
{ Scanner input = new Scanner(System.in);
System.out.println("1 = centimetre to inch, 2 = metres to feet, 3 = finish");
choice = Integer.parseInt(input.next());
if (choice == 1)
{
// converts centimetre to inch
System.out.println("centimetre to inch");
double centimetre;
double inch;
inch = 0;
centimetre = Double.parseDouble(input.next());
System.out.println("inch = centimetre * 0.3973");
}
else if (choice == 2)
{
// converts metre to feet
System.out.println("metre to feet");
double metre;
double feet;
feet = 0;
metre = Double.parseDouble(input.next());
System.out.println("feet = metre * 3.28");
}
else if (choice == 3)
{
// exits
break;
}
} while (choice != 3);
}
}
【问题讨论】:
-
将文件重命名为
AssessmentOne.java -
欢迎来到 Stack Overflow!我编辑了您问题的标题以包含确切的错误消息,因此更多了解该主题的人会看到它。我还将您的代码示例缩进了 4 个空格,以便正确呈现 - 请参阅编辑帮助以获取有关格式的更多信息。最后,我将您使用 Ideone 在线编译器的事实用粗体表示,因为我认为这在这种情况下很重要。祝你好运!
-
看起来 ideone 坚持你的公共类必须被称为 Main。请在此处查看此答案-您可能想考虑使用适当的完整开发环境,例如 Eclipse 或 IntelliJ stackoverflow.com/questions/22340309/