【问题标题】:No main class found when running program运行程序时找不到主类
【发布时间】:2021-07-25 12:36:15
【问题描述】:

我无法让这个程序运行:

练习 7.13 JHTP(日期类):创建一个名为 Date 的类 包括 3 个实例变量——一个月(int 类型)、一天(int 类型)、 和一年(类型 int)。提供一个初始化 3 实例变量并假设提供的值是正确的。 为每个实例变量提供一个 set 和一个 get 方法。提供一个 方法 displayDate 显示月份、日期和年份,由 正斜杠(/)。

我的代码:

public class Date {

    private int month;
    private int day;
    private int year;
    
    public Date(int month, int day, int year){//constructor
        this.month = month;//initilize 
        this.day = day;
        this.year = year;
    }
    
    public void setMonth(int month){
        this.month = month;//stores the names
    }
    public void setDay(int day){
        this.day = day;
    }
    public void setYear(int year){
        this.year = year;
    }
    public int getMonth(){
        return month;//return value

    }
    public int getDay(){
      return day;
    }
    public int getYear(){
        return year;
    }
    public String displayDate(){

        return month + "/" + day + "/" + "/" + year;
   

    }
}

我尝试添加public static void main(String[] args),但是我得到了一堆错误

【问题讨论】:

  • 您好,欢迎来到这里!我不确定 Stackoverflow 是否适合我们解决您的问题。无论如何,您必须提供有关错误的详细信息。

标签: java class main


【解决方案1】:

只需在你的类中添加一个 main 方法。

public class Date {

    private int month;
    private int day;
    private int year;

    public Date(int month, int day, int year){//constructor
        this.month = month;//initilize
        this.day = day;
        this.year = year;
    }

    public void setMonth(int month){
        this.month = month;//stores the names
    }
    public void setDay(int day){
        this.day = day;
    }
    public void setYear(int year){
        this.year = year;
    }
    public int getMonth(){
        return month;//return value

    }
    public int getDay(){
        return day;
    }
    public int getYear(){
        return year;
    }
    public String displayDate(){

        return month + "/" + day + "/" + "/" + year;


    }

    public static void main(String[] args) {
        Date date = new Date(11,11,1991);
        System.out.println(date.displayDate());
    }
}

如果您使用 InteliJ,要运行代码,请在 main 方法内右键单击“Run Date.main()”

这有利于测试目的,但在实际程序中,您将在包中的其他类中调用 Date 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2016-02-01
    • 1970-01-01
    • 2019-02-21
    • 2021-12-04
    • 2013-06-30
    相关资源
    最近更新 更多