用Java声明一个公共类

时间:2018-05-19 03:38:57

标签: 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

代码:

//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);
   }
}

1 个答案:

答案 0 :(得分:1)

您的类名为PastPresentFuture,我在错误消息中猜测您的文件不是PastPresentFuture.java。你需要把它改成那样匹配类名。

编辑:这仅适用于公共类。