<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                #### 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); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看