public class Leapyear{
int day;
void checkMonth(int i){
if(i%4==0&&i%100!=0||i%400==0){
day=29;
}
else
day=28;
}
void checkDay(int y,int x){
switch(y){
case 1:day=31;break;
case 2:this.checkMonth(x);break;
case 3:day=31;break;
case 4:day=30;break;
case 5:day=31;break;
case 6:day=30;break;
case 7:day=31;break;
case 8:day=31;break;
case 9:day=30;break;
case 10:day=31;break;
case 11:day=30;break;
case 12:day=31;break;
}
}
void show(){
System.out.println("一共有"+day);
}
public static void main(String args[]){
int year=Integer.parseInt(args[0]);
int month=Integer.parseInt(args[1]);
this.checkDay(year,month);
this.show();
}
}
无法编译...提示this.checkDay(year,month);
this.show();
有错误
请问如何在主方法中调用本类的成员函数,难道不能用this么,那到底该怎么调用
int day;
void checkMonth(int i){
if(i%4==0&&i%100!=0||i%400==0){
day=29;
}
else
day=28;
}
void checkDay(int y,int x){
switch(y){
case 1:day=31;break;
case 2:this.checkMonth(x);break;
case 3:day=31;break;
case 4:day=30;break;
case 5:day=31;break;
case 6:day=30;break;
case 7:day=31;break;
case 8:day=31;break;
case 9:day=30;break;
case 10:day=31;break;
case 11:day=30;break;
case 12:day=31;break;
}
}
void show(){
System.out.println("一共有"+day);
}
public static void main(String args[]){
int year=Integer.parseInt(args[0]);
int month=Integer.parseInt(args[1]);
this.checkDay(year,month);
this.show();
}
}
无法编译...提示this.checkDay(year,month);
this.show();
有错误
请问如何在主方法中调用本类的成员函数,难道不能用this么,那到底该怎么调用
Leapyear that = new Leapyear();
that.checkDay(year,month);
that.show();
你要调用checkDay()和show()先获得一个对象再说
import java.io.InputStreamReader;
import java.io.*;public class Leapyear{
int day;
void checkMonth(int i){
if(i%4==0&&i%100!=0||i%400==0){
day=29;
}
else
day=28;
}
void checkDay(int x,int y){
switch(y){
case 1:day=31;break;
case 2:this.checkMonth(x);break;
case 3:day=31;break;
case 4:day=30;break;
case 5:day=31;break;
case 6:day=30;break;
case 7:day=31;break;
case 8:day=31;break;
case 9:day=30;break;
case 10:day=31;break;
case 11:day=30;break;
case 12:day=31;break;
}
}
void show(int year,int month){
System.out.println(year+"年"+month+"月一共有"+day+"天");
}
public static void main(String args[]){
Leapyear leapyear=new Leapyear();
BufferedReader date=new BufferedReader(new InputStreamReader(System.in));
int year = 0;
int month=0;
try {
System.out.print("請輸入年份:");
year = Integer.parseInt(date.readLine());
System.out.print("請輸入月份:");
month=Integer.parseInt(date.readLine());
} catch (IOException ex) {
} catch (NumberFormatException ex) {
}
leapyear.checkDay(year,month);
leapyear.show(year,month); }
}
我也不太清楚,命令行參數是怎麼用的
但是樓主的方法顯然有問題
至少沒有輸入參數值
另外,調用類中的方法應該實例化對象,通過對象來調用類中的方法