天天看點

列印質數、随機字母、列印菱形、閏年閏月

小算法

  • 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