在Java中声明公共类
我已经运行了这段代码,并在网上寻找解决方案。如何修复此错误?我保存了PastPresentFuture.java,但它仍然无法运行在Java中声明公共类,java,public-method,Java,Public Method,我已经运行了这段代码,并在网上寻找解决方案。如何修复此错误?我保存了PastPresentFuture.java,但它仍然无法运行 YourName.java:7: error: class PastPresentFuture is public, should be declared in a file named PastPresentFuture.java public class PastPresentFuture ^ 1 error 代码: //名称 //日期 //章节
YourName.java:7: error: class PastPresentFuture is public, should be declared in a file named PastPresentFuture.java
public class PastPresentFuture
^
1 error
代码:
//名称
//日期
//章节与问题#
//应用程序提示用户输入月、日、年
导入java.util.*;
导入java.time.LocalDate;
公共类牧场未来
{
公共静态void main(字符串[]args)
{
LocalDate today=LocalDate.now();//创建今天的本地类
内莫,达,年;
int todayMo,todayDa,todayYr;
扫描仪输入=新扫描仪(System.in);
todayMo=today.getMonthValue();
todayDa=today.getDayOfMonth();
todayYr=today.getYear();
//输入月、日和年输入月、日和年的文本
系统输出打印(“五月”);
mo=input.nextInt();
系统输出打印(“18”);
da=input.nextInt();
系统输出打印(“2018”);
yr=输入.nextInt();
//参考第274页的if和else语句示例
//针对此问题,显示第295页上显示为(1,2,3,4)的文本语句
如果(年!=今天年)
系统输出打印项次(年+今日年);
其他的
如果(月<今日月)
系统输出打印项次(mo+今天mo);
其他的
如果(mo>今天mo)
系统输出打印项次(mo+今天mo);
其他的
系统输出打印项次(mo+todayDa);
}
}
您的类名为PastPresentFuture,我通过错误消息猜测您的文件不是PastPresentFuture.java。您需要将其更改为,使其与类名匹配
编辑:这仅适用于公共类。请注意,这仅适用于公共类,但最好为其声明的顶级类命名所有文件,无论是公共类还是其他类(例如,帮助您找到类的源)。@AndyTurner同意,我只是指出一个事实,如果你想更改非公共类的名称,你可以这样做。
//Name
//Date
//Chapter and problem #
//Application prompts user to enter month, day, year
import java.util.*;
import java.time.LocalDate;
public class PastPresentFuture
{
public static void main (String[] args)
{
LocalDate today = LocalDate.now(); //Creates the local class of today
int mo, da, yr;
int todayMo, todayDa, todayYr;
Scanner input = new Scanner(System.in);
todayMo = today.getMonthValue();
todayDa = today.getDayOfMonth();
todayYr = today.getYear();
//Input month, day and year enter text for month, day and year
System.out.print("May");
mo = input.nextInt();
System.out.print("18");
da = input.nextInt();
System.out.print("2018");
yr = input.nextInt();
//Reference if and else statement example on p. 274
//Display text statements shown as (1, 2, 3, 4) on p. 295 for this problem
if(yr != todayYr)
System.out.println(yr + todayYr);
else
if(mo < todayMo)
System.out.println(mo + todayMo);
else
if(mo > todayMo)
System.out.println(mo +todayMo);
else
System.out.println(mo +todayDa);
}
}