Java中常见程序:万年历,进制转换等

1.1/2+2/3+….+19/20

System.out.println("i\t\tm(i)");
        double a = 0;
        for (double i=1; i<=20; i++)
        {
            a += i/(i+1);
            System.out.println((int)i+"\t\t"+a);
        }
        System.out.println();

2.1000以内的完全数

//外层循环:每次循环获取到一个1~10000之间的数
        for (int i=1; i<=10000; i++)
        {
            //定义一个求和变量,用求外层循环获取到数的所有因数的和
            int sum = 1;
            //内层循环:求出从外层循环获取到的数的所有的因数
            for (int j=2; j<=i/2; j++)
            {
                //如果i除以j余数为0,说明j是i的因数把j加到sum里
                if (i%j==0)
                {
                    sum += j;
                }
                //如果j==i/2,说明内层循环即将结束,
                //找到了外层循环获取到的数的所有因数
                //并且sum等于i
                //说明i的所有因数的和等于i
                //i是完全数
                if (j==i/2&&sum==i)
                {
                    System.out.println(i);
                }
            }
        }

3.进制转换

public static void main(String[] args)  {
        //十进制转二进制
        String str1 = toBinary(4);
        System.out.println(str1);

        //二进制转十进制
        String str2 = binaryToDecimalism(11);
        System.out.println(str2);

        //十进制转八进制
        String str3 = toOctonary(9);
        System.out.println(str3);

        //十进制转十六进制
        String str4 = toSexadecimal(18);
        System.out.println(str4);
    }

    //十进制转二进制
    public static String toBinary(int a){
        StringBuffer sb = new StringBuffer();
        int i = 1;
        while (a!=0)
        {
            i = a % 2;
            sb.append(i);
            a /= 2;
        }
        sb.reverse();
        return new String(sb);
    }

    //二进制转十进制
    public static String binaryToDecimalism(int a){
        String str = a+"";
        int b = 1;
        int c = 0;
        int d = 0;
        for (int i=str.length()-1; i>=0; i--)
        {
            c = Integer.parseInt(str.charAt(i)+"")*b;
            d += c;
            b *= 2;
        }
        return d+"";
    }

    //十进制转八进制
    public static String toOctonary(int a){
        StringBuffer sb = new StringBuffer();
        int i = 1;
        while (a!=0)
        {
            i = a % 8;
            sb.append(i);
            a /= 8;
        }
        sb.reverse();
        return new String(sb);
    }

    //十进制转十六进制
    public static String toSexadecimal(int a){
        StringBuffer sb = new StringBuffer();
        int i = 1;
        while (a!=0)
        {
            i = a % 16;
            if (i-9>=1)
            {
                sb.append((char)(i-9+64));
            }
            else
            {
                sb.append(i);
            }
            a /= 16;
        }
        sb.reverse();
        return new String(sb);
    }

4.万年历

public static void main(String[] args) 
    {
        //1.输入年份和月份
        Scanner input = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = input.nextInt();
        System.out.println("请输入月份:");
        int month = input.nextInt();

        //2.判断年份是否是闰年
        boolean isLeapYear = leapYear(year);
        System.out.println("该年是否是闰年:" + isLeapYear);

        //3.判断输入月份对应的天数
        if(month>0 && month <= 12){
            int days = days(month, year);
            System.out.println("该月的天数是:" + days);
        }else {
            System.out.println("回你的火星吧");

        }

        //4.计算输入的年(1月1日)距1900年1月1日的天数
        int yearDays = getYearsDays(year);

        //5.输入的月份距当年1月1日的天数
        int monthDays = getMonthDays(year, month);

        //6.4+5
        int totalDays = yearDays + monthDays;

        //7.用总天数来计算输入月的第一天的星期数  +1是这个月第一天
        int week = (totalDays + 1) % 7;
        System.out.println("星期:" + week); //week是从0开始的

        //8.打印格式化日历
        formatterCalendar(week, year, month);

    }

    /*
    格式化日历
    需要传入星期 年 月
    */
    public static void formatterCalendar(int week, int year, int month){
        // 先打印表头
        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");

        // 定义一个变量 记录空格数
        //int count = 0;
        for(int i = 1; i <= week; i++){
            // 打印空格数
            System.out.print("\t");
        }

        for(int i = 1; i <= days(month, year); i++){
            // 打印日期按星期的排布
            System.out.print(i + "\t");

            // 七天之后 换行另起
            if((i + week) % 7 == 0){
                System.out.print("\n");
            }
        }
    }



    /*
    输入的月份的1日距输入的年份的1月1日的天数
    未知项 - 年份、月份
    返回值 - 天数
    */
    static int getMonthDays(int year, int month){

        int totalDays = 0;
        for(int x = 1; x < month; x++){
            totalDays += days(x, year);
        }
        return totalDays;
    }

    /*
    计算输入的年(1月1日)距1900年1月1日的天数
    未知项 - 输入的年份
    返回值 - 返回的总天数
    */
    public static int getYearsDays(int year){
        // 记录总天数
        int days = 0;
        for(int x = 1900; x < year; x++){
            days += leapYear(x)?366:365; 
        }
        return days;
    }

    /*
    判断输入的月的天数  -- 两个参数: 2月份平年 闰年天数不一样
    */
    public static int days(int month, int year){

        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            default:
                return leapYear(year)?29:28;    
        }
    }

    /*
    判断是否是闰年
    */
    public static boolean leapYear(int year){
        if((year%4==0&&year%100!=0) || year%400==0){
            return true;
        }
        return false;
    }

猜你喜欢

转载自blog.csdn.net/qq_22161527/article/details/75948839