如果只是为了完成题目按格式输出的要求,其实很简单,但这个题目主要用来练习封装的思想:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int year = s.nextInt();
int month = s.nextInt();
int day = s.nextInt();
TDate t = new TDate(year,month,day);
t.printDate();
}
}
class TDate{
private int year;
private int month;
private int day;
public void setYear(int year){
this.year = year;
}
public int getYear(){
return this.year;
}
public void setMonth(int month){
this.month = month;
}
public int getMonth(){
return this.month;
}
public void setDay(int day){
this.day = day;
}
public int getDay(){
return this.day;
}
public TDate(){
//默认的无参构造
this.year = 0;
this.month = 0;
this.day = 0;
}
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int year = s.nextInt();
int month = s.nextInt();
int day = s.nextInt();
TDate t = new TDate(year,month,day);
t.printDate();
}
}
class TDate{
private int year;
private int month;
private int day;
public void setYear(int year){
this.year = year;
}
public int getYear(){
return this.year;
}
public void setMonth(int month){
this.month = month;
}
public int getMonth(){
return this.month;
}
public void setDay(int day){
this.day = day;
}
public int getDay(){
return this.day;
}
public TDate(){
//默认的无参构造
this.year = 0;
this.month = 0;
this.day = 0;
}
// 有参构造
public TDate(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.day+"/"+this.month+"/"+this.year);
}
}
public TDate(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.day+"/"+this.month+"/"+this.year);
}
}