🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### 1.万年历 ``` // 萬年歷,1900-1-1是週一。 public static void calendar() { Scanner sc = new Scanner(System.in); System.out.println("請輸入年份"); int year = sc.nextInt(); System.out.println("請輸入月份"); int month = sc.nextInt(); // 計算year年(month-1)月到1900-1-1總共有幾天。 int total = 0; for (int i = 1900; i < year; i++) { if (i % 400 == 0 || (i % 100 != 0 && i % 4 == 0)) { total += 366; } else { total += 365; } } for (int i = 1; i < month; i++) { int days = 31; switch (i) { case 4: case 6: case 9: case 11: days = 30; break; case 2: if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { days = 29; } else { days = 28; } } total += days; } // year年month月的第一天是星期几。 int firstDay = total % 7 + 1; firstDay = (firstDay % 7 == 0) ? 0 : firstDay; // 获得month月的天数。 int currentMonthDays = 31; switch (month) { case 4: case 6: case 9: case 11: currentMonthDays = 30; break; case 2: if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { currentMonthDays = 29; } else { currentMonthDays = 28; } } int count = firstDay + currentMonthDays; System.out.println("日\t一\t二\t三\t四\t五\t六"); for (int i = 0; i < count; i++) { int j = i - firstDay + 1; if (j < 1) { System.out.print("\t"); } else { System.out.print(j + "\t"); } if ((i + 1) % 7 == 0) { System.out.println(); } } } ``` #### 2.最大公约数&最小公倍数 `两个数的乘积等于这两个数的最大公约数与最小公倍数的乘积` ``` public class demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入第一个整数:"); int num1 = sc.nextInt(); System.out.print("请输入第二个整数:"); int num2 = sc.nextInt(); /*if(num1>num2){ System.out.println(test(num2,num1)); }else{ System.out.println(test(num1,num2)); }*/ System.out.println(test1(num1,num2)); //最小公倍数 System.out.println((num1*num2)/test1(num1,num2)); } //辗转相除 public static int test(int a,int b){ if(a%b==0){ return b; }else{ return test(b,a%b); } } //更相减损 public static int test1(int a,int b){ if(a==b){ return a; }else if(a<b){ return test1(b-a,a); }else{ return test1(a-b,b); } } } ``` #### 3.质数 ``` public static void prime(){ Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); int num = sc.nextInt(); if(num==1){ System.out.println("不是一个质数"); }else{ double k = Math.sqrt(num); int i = 2; for(;i<=k;i++){ if(num%i==0){ break; } } if(i<=k){ System.out.println("不是一个质数"); }else{ System.out.println("是一个质数"); } } } ``` #### 4.杨辉三角 ``` public static void test() { int[][] arr = new int[10][10]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < i + 1; j++) { if (j == 0) { arr[i][j] = 1; } else { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } } } for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] != 0) { System.out.print(arr[i][j] + "\t"); } } System.out.println(); } } ``` #### 5."小技巧" ``` //从控制台循环的输入数个正整数。输入-1表示结束。 循环结束后,所有数的最大的数和最小的数。 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int max = 0, min = 0; for (int i = 1;; i++) { System.out.println("请输入第" + i + "个数:"); int n = sc.nextInt(); if (n == -1) break; if (i == 1) { max = n; min = n; } max = n > max ? n : max; min = n < min ? n : min; } System.out.println("最大数:" + max); System.out.println("最小数:" + min); } ``` #### 6.排序 ``` //冒泡排序 public static void sort() { int[] arr = { 1, 2, 3, 4, 55, -7 }; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j + 1] < arr[j]) { int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i] + "\t"); } } ``` #### 7.把数组中大于arr[0]的元素移动到其前,小于的移动到其后。 ``` public static void demo() { int[] arr = { 3, 5, 2, 8, 6, 7, 1}; int flagIndex = 0; for (int i = 1; i < arr.length; i++) { if(arr[i]>arr[flagIndex]){ for (int j = i; j > flagIndex; j--) { int temp = arr[j]; arr[j]= arr[j-1]; arr[j-1] = temp; } flagIndex++; } } System.out.println(Arrays.toString(arr)); } ``` #### 8.在str1中删除str2,统计删除str2个数和删除str2后的str1. ``` public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入字符串:"); String str1 = sc.next(); System.out.print("请输入你要删除的字符串:"); String str2 = sc.next(); del(str1, str2); } public static void del(String str1, String str2) { StringBuilder sb1 = new StringBuilder(str1); int count = 0; int len = str2.length(); int index = sb1.indexOf(str2); while (index != -1) { count++; sb1.delete(index, index + len); index = sb1.indexOf(str2); // 不能用index = sb1.indexOf(str2,index+len);因为已经把li删除了 } System.out.println("删除后:" + sb1); System.out.println("删除个数:" + count); } } ```