首页 > 编程语言 > 详细

JAVA 第五周上机练习4.2

时间:2020-04-02 12:38:23      阅读:37      评论:0      收藏:0      [点我收藏+]

1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。

package one;

public class Test02 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num = 100;
        int a, b, c;
        for (num = 100; num < 1000; num++) {
            a = num % 10;
            b = num / 10 % 10;
            c = num / 100 % 10;
            if (num == a * a * a + b * b * b + c * c * c) {
                System.out.println(num);
            }
        }

    }

}

2.在控制台输出以下图形(知识点:循环语句、条件语句)

package one;

public class Test02 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int a = 1; a <= 6; a++) {
            for (int i = 1; i <= a; i++) {
                System.out.printf("%d ", i);
            }
            System.out.print("\n");
        }
        System.out.print("\n");
        for (int a = 6; a >= 1; a--) {
            for (int i = 1; i <= a; i++) {
                System.out.printf("%d ", i);
            }
            System.out.print("\n");
        }
        System.out.print("\n");

        for (int a = 1; a <= 6; a++) {
            for (int i = 1; i <= 2 * (6 - a); i++) {
                System.out.print("");
            }
            for (int i = a; i >= 1; i--) {
                System.out.printf("%d ", i);
            }
            System.out.print("\n");
        }
        System.out.print("\n");

        for (int a = 6; a >= 1; a--) {
            for (int i = 1; i <= 2 * (6 - a); i++) {
                System.out.print("");
            }
            for (int i = 1; i <= a; i++) {
                System.out.printf("%d ", i);
            }
            System.out.print("\n");
        }
    }

}

 

3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)

package one;

import java.util.Scanner;

public class Test01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int year, month, day, a = 0, b = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("请输入年份");
        year = input.nextInt();
        System.out.println("请输入月份");
        month = input.nextInt();
        System.out.println("请输入日");
        day = input.nextInt();
        for (int i = 1; i < month; i++) {
            switch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                a = 31;
                break;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    a = 29;
                } else {
                    a = 28;
                    break;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                a = 30;
                break;
            }
            b = b + a;
        }
        b = b + day;
        System.out.println("这是这一年的第" + b + "天");
    }

}

 

4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321

package one;

import java.util.Scanner;

public class Test01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a, b, c, d, e, f;
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个四位整数");
        e = input.nextInt();
        a = e / 1000;
        b = e / 100 % 10;
        c = e / 10 % 10;
        d = e % 10;
        f = d * 1000 + c * 100 + b * 10 + a;
        System.out.println("反转后的数位" + f);

    }

}

 

JAVA 第五周上机练习4.2

原文:https://www.cnblogs.com/y18741378909/p/12618561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!