关于用 java 语言编写的万年历的解读
阅读下面这条关于万年历的java语言,解读中间的那条“sum+=1;”的作用及其表达的意思。感谢!由于直接复制程序,看得有点眼乱,先上图(程序是刚好衔接的):import...
阅读下面这条关于万年历的 java 语言,解读中间的那条 “ sum += 1; ” 的作用及其表达的意思。
感谢!
由于直接复制程序,看得有点眼乱,先上图(程序是刚好衔接的):
import java.util.Scanner;
public class Test4262{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year;
System.out.println("请输入年份");
year = in.nextInt();
System.out.println("请输入月份");
int month = in.nextInt();
int sum = 0;
for(int y = 1900 ; y < year ; y++){
if(y % 4 == 0 && y % 100 !=0 || y % 400 ==0){
sum += 366 ;
}else{
sum += 365;
}
}
for(int m = 1 ; m < month ; m++){
if(m == 2){
if(year % 4 == 0 && year % 100 !=0 || year % 400 ==0){
sum += 29;
}else{
sum += 28;
}
}else{
if(m == 4 || m == 6 || m == 9 || m == 11){
sum += 30;
}else{
sum += 31;
}
}
}
sum += 1;
int wekday = sum % 7;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i = 1 ; i <= wekday ; i++){
System.out.print("\t");
}
int t = 0 ;
if(month == 2){
if(year % 4 == 0 && year % 100 !=0 || year % 400 ==0){
t = 29;
}else{
t = 28;
}
}else{
if(month == 4 || month == 6 || month == 9 || month == 11){
t = 30;
}else{
t = 31;
}
}
for(int i = 1 ; i <= t ; i++){
if(sum % 7 == 6){
System.out.print(i+"\n");
}else{
System.out.print(i+"\t");
}
sum++;
}
System.out.println("\n");
}
} 展开
感谢!
由于直接复制程序,看得有点眼乱,先上图(程序是刚好衔接的):
import java.util.Scanner;
public class Test4262{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year;
System.out.println("请输入年份");
year = in.nextInt();
System.out.println("请输入月份");
int month = in.nextInt();
int sum = 0;
for(int y = 1900 ; y < year ; y++){
if(y % 4 == 0 && y % 100 !=0 || y % 400 ==0){
sum += 366 ;
}else{
sum += 365;
}
}
for(int m = 1 ; m < month ; m++){
if(m == 2){
if(year % 4 == 0 && year % 100 !=0 || year % 400 ==0){
sum += 29;
}else{
sum += 28;
}
}else{
if(m == 4 || m == 6 || m == 9 || m == 11){
sum += 30;
}else{
sum += 31;
}
}
}
sum += 1;
int wekday = sum % 7;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i = 1 ; i <= wekday ; i++){
System.out.print("\t");
}
int t = 0 ;
if(month == 2){
if(year % 4 == 0 && year % 100 !=0 || year % 400 ==0){
t = 29;
}else{
t = 28;
}
}else{
if(month == 4 || month == 6 || month == 9 || month == 11){
t = 30;
}else{
t = 31;
}
}
for(int i = 1 ; i <= t ; i++){
if(sum % 7 == 6){
System.out.print(i+"\n");
}else{
System.out.print(i+"\t");
}
sum++;
}
System.out.println("\n");
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询