【问题标题】:Declaring a public class in Java [duplicate]在Java中声明一个公共类[重复]
【发布时间】:2018-10-29 11:56:30
【问题描述】:

我已运行此代码并在网上寻找解决方案。我该如何解决这个错误?我保存了 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);
   }
}

【问题讨论】:

标签: java public-method


【解决方案1】:

您的课程名为 PastPresentFuture,我根据错误消息猜测您的文件不是 PastPresentFuture.java。您需要将其更改为与类名匹配。

编辑:这仅适用于公共课程。

【讨论】:

  • 这仅对公共类是必需的,但最好为它声明的顶级类命名所有文件,无论是公共的还是其他的(例如,帮助您找到一个类的源代码)。
  • @AndyTurner 同意,我只是指出,如果您想更改非公共类的名称,您可以这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 2013-08-09
  • 2010-12-06
相关资源
最近更新 更多