zoukankan      html  css  js  c++  java
  • 打印质数、随机字母、打印菱形、闰年闰月

    • 1——100求和
    //方法一:
    public
    class Demo { public static void main(String[] args) { int result = 0; for (int i=0; i<101; i++) { result += i; } System.out.println(result); } }
    //方法二:
    public class Demo {
        public static void main(String[] args) {
            int sum = sum(100);
            System.out.println(sum);
        }
        static int sum(int n) {
            if (n==0) {
                return 0;
            }
            return n+sum(n-1);
        }
    }
    • 1——100偶数和
    //方法一:
    public class Demo {
        public static void main(String[] args) {
            int result = 0;
            for (int i=0; i<101; i=i+2) {
                result += i;
            }
            System.out.println(result);
        }
    }
    //方法二:
    public class Demo {
      public static void main(String[] args) {
          int sum = sum(100);
          System.out.println(sum);
      }
      static int sum(int n) {
          if (n==0) {
              return 0;
          }
          return n+sum(n-2);
      }
    }
    • 1——100的质数
    public class Demo {
        public static void main(String[] args) {
            for (int i = 1; i < 101; i++) {
                boolean flag = true;
                if (i == 1) {
                    continue;
                }
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    System.out.println(i);
                }
            }
        }
    }

    • 1——100 的合数
    public class Demo {
        public static void main(String[] args) {
            for (int i = 1; i < 101; i++) {
                boolean flag = true;
                if (i == 1) {
                    continue;
                }
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        flag = false;
                        break;
                    }
                }
                if (!flag) {//只改这一个地方就好了
                    System.out.println(i);
                }
            }
        }
    }
    •  生成大小写随机字母
    package HelloWorld;

    public class HelloWorld {
        public static void main(String[] args) {
            char random;
            do {
                random = (char) (Math.random() * 1000 + 65);
            } while (random > 122 ||( random > 90 & random < 98));
            
            System.out.println(random);
        }
    }
    •  打印菱形
        public static void main(String[] args) {
            for (int i=0; i<20; i++) {
                for (int w=20; w>0; w--) {
                    System.out.print(" ");
                }
                for (int k=19; k>i; k--) {
                    System.out.print(" ");
                }
                for (int j=0; j<i+1; j++) {
                    System.out.print("*");
                }
                for (int h=0; h<i; h++) {
                    System.out.print("*");
                }
                System.out.println();
                
            }
            for (int i=0; i<19; i++) {
                for (int w=20; w>0; w--) {
                    System.out.print(" ");
                }
                for (int j=0; j<i+1; j++) {
                    System.out.print(" ");
                }
                for (int k=19; k>i+0; k--) {
                    System.out.print("*");
                }
                for (int h=0; h<18-i; h++) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    •  平年闰年每月多少天
    public class Test {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.println("请输入一个年份");
                int year = scanner.nextInt();
                if (year == -1) {
                    close(scanner);
                    break;
                }
                System.out.println("请输入一个月份");
                int month = scanner.nextInt();
                if (year == -1) {
                    close(scanner);
                    break;
                }
                boolean leapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
                if (leapYear) {
                    System.out.print(year + "年是闰年,");
                    if (month == 2)
                        month = 0;
                } else {
                    System.out.print(year + "年是平年,");
                }
                switch (month) {
                case 0:
                    System.out.println("2月有" + "29天");
                    break;
                case 2:
                    System.out.println(month + "月有" + "28天");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    System.out.println(month + "月有" + "30天");
                    break;
                default:
                    System.out.println(month + "月有" + "31天");
                }
            }
        }
        public static void close(Scanner scanner) {
            scanner.close();
            System.err.println("程序退出!");
        }
    }
    View Code
  • 相关阅读:
    O(n^2)的排序方法
    99乘法表
    excel 转 csv
    批量关闭 excel
    tomcat 加入服务
    文件打包 zip
    字符串转换
    List数组种删除数据
    mybatis 批量上传
    sql server 查询表字段及类型
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/6752636.html
Copyright © 2011-2022 走看看