• Java第五次上机作业


    1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)

    package com.a01;
    
    import java.util.*;
    
    public class hellowold {
        public static void main(String[] args) {
            System.out.println("输入一个三位数");
            Scanner input = new Scanner(System.in);
            int n = input.nextInt();
            int ge, shi, bai, i;
            for (i = 100; i <= 999; i++) {
                if (n >= 100 && n <= 999) {
                    ge = n % 100 % 10;
                    shi = n % 100 / 10;
                    bai = n / 100;
    
                    System.out.println("个位,十位,百位的立方和为:" + (ge * ge * ge + shi * shi * shi + bai * bai * bai));
                    break;
                }
                System.out.println("输入不合法,请重新输入");
                n = input.nextInt();
            }
    
        }
    }

     

    2.

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

    package com.a01;
    
    public class hellowold {
        public static void main(String[] args) {
            int a, b;
            for (a = 1; a <= 6; a++) {
                for (b = 1; b <= a; b++) {
                    System.out.print(b);
                }
                System.out.println("
    ");
            }
    
        }
    }

    package com.a01;
    
    public class hellowold {
        public static void main(String[] args) {
            int a, b;
            for (a = 6; a >= 1; a--) {
                for (b = 1; b <= a; b++) {
                    System.out.print(b);
                }
                System.out.println("
    ");
            }
    
        }
    }

     

    package com.a01;
    
    public class hellowold {
        public static void main(String[] args) {
            int a, b;
            for (a = 1; a < 7; a++) {
                for (b = 7; b > 0; b--) {
                    if (b <= a) {
                        System.out.print(b);
                    } else {
                        System.out.print(" ");
                    }
    
                }
                System.out.println("
    ");
            }
    
        }
    }

     

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

    import java.util.*;
    
    public class hellowold {
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
            System.out.println("year");
            int year = input.nextInt();
            System.out.println("mouth");
            int month = input.nextInt();
            System.out.println("day");
            int day = input.nextInt();
            int total = 0;
            for (int i = 1; i < month; i++) {
    
                switch (i) {
                case 4:
                case 6:
                case 9:
                case 11:
                    total += 30;
                    break;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    
                        total += 29;
                    else
                        total += 28;
                    break;
                default:
                    total += 31;
                    break;
                }
    
            }
            total += day;
            System.out.println("该天是第" + total + "天");
    
        }
    }

    4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)

    package com.a01;
    
    import java.util.*;
    
    public class hellowold {
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
            System.out.println("输入一个四位数");
            int n = input.nextInt();
            int a,b,c,d,i,t,k;
            a:for(i=1000;i<=9999;i++){
                
                    a=n%100%10;
                    b=n%100/10;
                    c=n%1000/100;
                    d=n/1000;
                    t=a;
                    a=d;
                    d=t;
                    k=b;
                    b=c;
                    c=k;
                    System.out.println("转化为:"+d+c+b+a);
                    break a;
                
            }
            
            
        }
    }

  • 相关阅读:
    Ubuntu18.04下搭建LNMP教程-超详细图文(Nginx+MySQL+PHP含各种解决报错问题)
    win10下Ubuntu18.04安装的简单教程
    Bugku-CTF加密篇之富强民主
    PHP中PHP $_POST和PHP $_REQUEST及PHP $_GET的用法及区别
    Base系列编码浅析【base16 base32 base64 base85 base36 base 58 base91 base 92 base62】
    Bugku-CTF加密篇之贝斯家族(@iH<,{bdR2H;i6*Tm,Wx2izpx2!)
    Bugku-CTF加密篇之这不是md5(666c61677b616537333538376261353662616566357d)
    Bugku-CTF加密篇之告诉你个秘密(ISCCCTF)
    攻防世界—pwn—guess_num
    攻防世界—pwn—level2
  • 原文地址:https://www.cnblogs.com/hyonf/p/12619837.html
Copyright © 2020-2023  润新知