[英]No main class found when running program
我無法讓這個程序運行:
練習 7.13 JHTP(日期類):創建一個名為 Date 的 class,其中包括 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)
但是,然后我得到了一堆錯誤
只需在 class 中添加一個主要方法。
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 方法內右鍵單擊並“運行 Date.main()”
這有利於測試目的,但在實際程序中,您將在 package 的其他類中調用 Date class。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.