第五周上机作业

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

import java.util.*;
public class lqh {
     public static void main(String[] args) {
                for (int i = 100; i <= 999; i++) {
                    int ge = i % 10;
                    int shi = (i/10) % 10;
                    int bai = (i/100) % 10;
                    int sum = ge*ge*ge + shi*shi*shi + bai*bai*bai; 
                    if (sum == i) {
                        System.out.println(i);
                    }
                }
            }
    }

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

import java.util.*;
public class lqh {
     public static void main(String[] args) {
            for(int i=1;i<=7;i++) {
                for(int j=1;j<i;j++) {
                    System.out.print(j);
                }
                System.out.println();
            }
        }
    }

 

import java.util.*;
public class lqh {
      public static void main(String[] args) {
            for(int i=6;i>=0;i--) {
                for(int j=1;j<=i;j++) {
                    System.out.print(j);
                }
                System.out.println();
            }
        }
    }

 

 

import java.util.*;
public class lqh {
     public static void main(String[] args) {
                System.out.println("     1\n    21\n   321\n  4321\n 54321\n654321");
            }
        }

 

import java.util.*;
public class lqh {
      public static void main(String[] args) {
            for(int i=6;i>0;i--) {
                for(int k=0;k<6-i;k++) {
                    System.out.print(" ");
                }
                for(int j=1;j<=i;j++) {
                    System.out.print(j);
                }
                System.out.println("");
            }
        }
    }

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

import java.util.*;
public class lqh {
      public static void main(String[] args) {
              int year,mouth,days;
                int day = 0;
                int a = 0; 
                int b = 0;
                Scanner scanner = new Scanner(System.in);
                do {
                    System.out.println("输入年:");
                    year = scanner.nextInt();
                    System.out.println("输入月:");
                    mouth = scanner.nextInt();
                    System.out.println("输入日:");
                    days = scanner.nextInt();
                    if (year < 0 || mouth < 0 || mouth > 12 || days < 0 || days > 31) {
                        System.out.println("输入错误请重新输入!");
                        b = 1;
                    }
                } while (b == 1);
                
                for (int i = 1; i < mouth; i++) {
                    switch (i) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12: {
                        day = 31;
                        break;
                    }
                    case 4:
                    case 6:
                    case 9:
                    case 11: {
                        day = 30;
                        break;
                    }
                    case 2: {
                        if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
                            day = 29;
                        } else {
                            day = 28;
                        }
                    }
                    default:
                        break;
                    }
                    a += day;
                }
                System.out.println("这是" + year + "年的" + (a + days) + "天");
            }
        }

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

import java.util.*;
public class lqh {
      public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("输入四位数字");
            int n=sc.nextInt(); 
            int ge = n / 1000; 
            int shi = n / 100 % 10; 
            int bai = n / 10 % 10; 
            int qian = n % 10; 
            int s = qian * 1000 + bai * 100 + shi * 10 + ge; 
            System.out.println("反转后=" + s); 
        }
    }

 

posted @ 2020-04-02 13:11  吕清华  阅读(193)  评论(0编辑  收藏  举报