java学习第42天2020/8/16
一.
import java.util.Scanner; public class time { int year,month,day; time() { year=1000;month=1;day=1; } public int isleap() { if(year%400==0||year%100!=0&&year%4==0) return 1; else return 0; } public int check() { if(month<=12) { if((month==2&&isleap()==1&&day<=29)||(month==2&&isleap()==0&&day<=28)||((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<31)||(month==4||month==6||month==9||month==11&&day==30)) return 1; else return 0; } else return 0; } public void setdate(int j,int k,int l) { year=j; month=k; day=l; } public void display() { if(check()==0) System.out.println(year+"-"+month+"-"+day+","+"error day"); else { System.out.println(year+"-"+month+"-"+day); if(isleap()==1) System.out.println(","+"leap day"); else System.out.println(","+"common year"); } } public static void main(String []args) { Scanner input=new Scanner(System.in); time r=new time(); System.out.println("请输入两组年月日:"); int a,b,c,d,e,f; a=input.nextInt();b=input.nextInt();c=input.nextInt();d=input.nextInt();e=input.nextInt();f=input.nextInt(); r.isleap(); r.check(); r.display(); r.setdate(a,1,1); r.isleap(); r.check(); r.display(); r.setdate(a,b,1); r.isleap(); r.check(); r.display(); r.setdate(a,b,c); r.isleap(); r.check(); r.display(); r.setdate(d,e,f); r.isleap(); r.check(); r.display(); } }
二.输出后都会换行
使用System.out.println输出换行使用System.out.print输出不换行
package 例题; import java.util.Scanner; public class time { int year,month,day; time() { year=1000;month=1;day=1; } public int isleap() { if(year%400==0||year%100!=0&&year%4==0) return 1; else return 0; } public int check() { if(month<=12) { if((month==2&&isleap()==1&&day<=29)||(month==2&&isleap()==0&&day<=28)||((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<31)||(month==4||month==6||month==9||month==11&&day==30)) return 1; else return 0; } else return 0; } public void setdate(int j,int k,int l) { year=j; month=k; day=l; } public void display() { if(check()==0) System.out.println(year+"-"+month+"-"+day+","+"error day"); else { System.out.print(year+"-"+month+"-"+day); if(isleap()==1) System.out.println(","+"leap day"); else System.out.println(","+"common year"); } } public static void main(String []args) { Scanner input=new Scanner(System.in); time r=new time(); System.out.println("请输入两组年月日:"); int a,b,c,d,e,f; a=input.nextInt();b=input.nextInt();c=input.nextInt();d=input.nextInt();e=input.nextInt();f=input.nextInt(); r.isleap(); r.check(); r.display(); r.setdate(a,1,1); r.isleap(); r.check(); r.display(); r.setdate(a,b,1); r.isleap(); r.check(); r.display(); r.setdate(a,b,c); r.isleap(); r.check(); r.display(); r.setdate(d,e,f); r.isleap(); r.check(); r.display(); } }
三.例题