根据以下提供的Nextdate问题的伪代码,用C或C++,JAVA语言编写程序

Program NextDate1 ‘Simple version

DimtomorrowDay,tomorrowMonth,tomorrowYear As Integer
Dim day,month,year As integer

Output(“Enter today’s date inform MM DD YYYY”)
Input(month,day,year)
Case month of
Case 1: month Is 1,3,5,7,8, Or10:’31 day months(except Dec.)
If day<31
Then tomorrowDay = day+1
Else
TomorrowDay=1
TomorrowMonth=month+1
Endif
Case 2: month Is 4,6,9, or 11 ’30 day months
If day<30
Then tomorrowDay=day+1
Else
TomorrowDay=1
TomorrowMonth=month+1
Endif
Case 3: month is 12:’December
If day<31
Then tomorrowDay=day+1
Else
TomorrowDay=1
TomorrowMonth=1
If year=2012
Then Output(“2012 is over”)
Else tomorrow.year=year+1
Endif
Case 4:month is 2:’February
If day<28
ThentomorrowDay=day+1
Else
If day=28
Then
If (year is a leap year)
Then tomorrowDay=29 ‘ leap year
Else ‘not a leap year
TomorrowDay=1
TomorrowMonth=3
Endif
Else if day=29
Then tomorrowDay=1
TomorrowMonth=3
Else output(“Cannot have Feb.”,day)
Endif
Endif
Endif
Endcase
Output(“Tomorrow’s date is”,tomorrowMonth, tomorrowDay, tomorrowYear)
End Nextdate

第1个回答  2013-10-15

Java代码实现如下:


import java.util.Scanner;

public class NextDate {

    //判断某一年是不是闰年的函数
    public static boolean leapYear(int year){
        if((year%4==0&&year%100!=0)||(year%400==0)){
            //是闰年
            return true;
        }
        return false;
    }
    
    public static void main(String[] args) {
        System.out.println("Enter today’s date inform MM DD YYYY");
        Scanner sc = new Scanner(System.in);
        int month = sc.nextInt();
        int day = sc.nextInt();
        int year = sc.nextInt();
        int tomorrowDay  = 0;
        int tomorrowMonth  = 0;
        if(month==1){//case 1
            if(day<31)
                tomorrowDay = day+1;
            else{
                tomorrowDay = 1;
                tomorrowMonth = month+1;
            }
        }else if(month==4){//case 2
            if(day<30){
                tomorrowDay = day+1;
            }else{
                tomorrowDay = 1;
                tomorrowMonth=month+1;
            }
        }else if(month==12){//case 3
            if(day<31){
                tomorrowDay = day+1;
            }else{
                tomorrowDay=1;
                tomorrowMonth=1;
            }
            if(year==2012){
                System.out.println("2012 is over");
            }else{
                year=year+1;
            }
        }else if(month==2){//case 4
            if(day<28){
                tomorrowDay=day+1;
            }else{
                if(day==28){
                    if(leapYear(year)){//是不是闰年的函数
                        tomorrowDay = 29;
                    }else{
                        tomorrowDay=1;
                        tomorrowMonth=3;
                    }
                }else if(day==29){
                    tomorrowDay=1;
                    tomorrowMonth=3;
                }else{
                    System.out.println("Cannot have Feb."+day);
                }
            }
        }
    }

}


运行结果如图:



Warning: Invalid argument supplied for foreach() in /www/wwwroot/aolonic.com/skin/templets/default/contents.html on line 45
相似回答