Java演算法基本功練習
該篇為Java演算法基本功小練習,適合初學者練手的題。
1.列印九九乘法表。
/** * 列印九九乘法表 */ public static void multiplicationTable() { for (int i = 1; i <= 9; i++) {//用i控制行,1-9行 for (int j = 1; j <= i; j++) {//用j控制列,j最大不超過當前i System.out.print(i + "*" + j + "=" + i*j + "\t"); } System.out.println(); } }
執行結果:

2.定義一個二維陣列,int[2][4],要求是迴圈輸入8個整數,存入到陣列中,然後輸出這個陣列中的最大值。
/** * 定義一個二維陣列,int[2][4],要求是迴圈輸入8個整數,存入到陣列中,然後輸出這個陣列中的最大值。 */ public static void arrayMax() { Scanner scanner = new Scanner(System.in); int[][] array = new int[2][4]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < 4; j++) { System.out.println("請輸入" + i + "行" + j + "列數字"); array[i][j] = scanner.nextInt();//獲取輸入數存在對應行列位置 } } int max = array[0][0]; //假定第一個數最大 for (int i = 0; i < array.length; i++) { for (int j = 0; j < 4; j++) { if (array[i][j] > max) {//有更大就替換 max = array[i][j]; } } } System.out.println("最大數為:" + max); }
執行結果:

3.定義一個長度為10的整型陣列,迴圈輸入10個整數。然後判斷這個陣列中有幾個偶數,再定義一個正好能存放這幾個偶數的陣列,將上一個陣列中的所有偶數複製過來。最後迴圈輸出這些偶數。
/** * 定義一個長度為10的整型陣列,迴圈輸入10個整數。然後判斷這個陣列中有幾個偶數,再定義一個正好能存放這幾個偶數的陣列,將上一個陣列中的所有偶數複製過來。最後迴圈輸出這些偶數。 */ public static void even() { int[] array = new int[10]; Scanner scanner = new Scanner(System.in); int evenCount = 0; for (int i = 0; i < array.length; i++) { System.out.println("輸入第" + (i+1) + "整數"); array[i] = scanner.nextInt(); if (array[i] % 2 == 0) { evenCount++;//輸入的同時判斷是否為偶數 } } int[] evenArray = new int[evenCount]; evenCount = 0; for (int i = 0; i < array.length; i++) { if (array[i] % 2 == 0) { evenArray[evenCount] = array[i]; evenCount++;//記錄新偶數陣列存放個數 } } System.out.print("所有的偶數有: "); for (int i = 0; i < evenCount; i++) { System.out.print(evenArray[i] + " "); } }
執行結果:

4.輸入一個數判斷一個數是否為質數(只能被1和本身整除的數叫質數)
/** * 輸入一個數判斷一個數是否為質數(只能被1和本身整除的數叫質數) */ public static void primeNumber() { Scanner scanner = new Scanner(System.in); System.out.println("請輸入一個數:"); int num = scanner.nextInt(); int count = 0;//記錄能被整除的數的個數 for (int i = 1; i <= num; i++) { if (num % i == 0) { count++; } } if (count < 3) { System.out.println("這個數是質數"); }else { System.out.println("這個數不是質數"); } }
執行結果:

5.定義一個二維陣列,用來記錄3個學生的java,c#,sql三門功課的成績,二維陣列的一行記錄一個人的成績
/** * 定義一個二維陣列,用來記錄3個學生的java,c#,sql三門功課的成績,二維陣列的一行記錄一個人的成績,要求迴圈輸入,最後輸出格式如下: * java c#sql * 第1名學生897998 * 第2名學生9980100 * 第3名學生799987 */ public static void printScore() { int[][] array = new int[3][3]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < array.length; i++) { int j = 0; System.out.println("請輸入第" + (i+1) + "個學生java成績:"); array[i][j++] = scanner.nextInt(); System.out.println("請輸入第" + (i+1) + "個學生c#成績:"); array[i][j++] = scanner.nextInt(); System.out.println("請輸入第" + (i+1) + "個學生sql成績:"); array[i][j++] = scanner.nextInt(); } System.out.println("java\tc#\tsql"); for (int i = 0; i < array.length; i++) { System.out.print("第" + (i+1) + "名學生成績\t"); for (int j = 0; j < 3; j++) { System.out.print(array[i][j] + "\t"); } System.out.println(); } }

6.計算1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10=?
/** * 計算1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10=? */ public static void accumulation() { double sum = 0; for (int i = 1; i <= 10; i++) { sum += (double) 1/i; } System.out.print(sum); }
7.從1不斷的累加,最多加到100,但如果你的累加結果正好大於1000時,要求也結束這個迴圈。問這個時候正好加到了幾。
/** * 從1不斷的累加,最多加到100,但如果你的累加結果 * 正好大於1000時,要求也結束這個迴圈。問這個時候正好加 * 到了幾。 */ public static void accumulation() { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; if (sum > 1000) { System.out.print("這個時候正好加到:" + i); break; } } }
結果為:
這個時候正好加到:45
8.從1累加到100,但如果要累加的資料能被3整除,並且也能被7整數,那麼就不要累加這個數。最後輸出結果。
/** * 從1累加到100,但如果要累加的資料能被3整除, * 並且也能被7整數,那麼就不要累加這個數。最後輸出結果。 */ public static void accumulation2() { int sum = 0; for (int i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 7 == 0) { continue; } sum += i; } System.out.print("總和為:" + sum); }
結果為:
總和為:4840
9.列印如下圖形
/** * 列印圖形 * * * *** * ***** * ******* * ********* * *********** * ************* */ public static void printStart() { //總共有7行 for (int i = 0; i < 7; i++) { int printCount = 1 + 2*i;//每行列印個數規律為1+2*n for (int j = 0; j < printCount; j++) { System.out.print("*");//迴圈列印 } System.out.println();//換行 } }
結果為:

10.列印如下圖形
/** * * * *** * ***** * ******* * ********* * *********** * ********* * ******* * ***** * *** * * */ public static void printStar2() { //列印前6行 for (int i = 0; i < 6; i++) { int printCount = 1 + 2*i;//每行列印個數規律為1+2*n for (int j = 0; j < printCount; j++) { System.out.print("*");//迴圈列印 } System.out.println();//換行 } //列印後5行 for (int i = 4; i >= 0; i--) { int printCount = 1 + 2*i; for (int j = 0; j < printCount; j++) { System.out.print("*"); } System.out.println(); } }

11.給一個數組做反序。
/** * 給一個數組做反序。 */ public static void reserseArray() { //採用陣列對稱依次相互交換值得做法 int[] array = new int[]{13,0,6,42,8,24,9,10}; int start = 0;//低位下標 int end = array.length-1;//高位下標 for (; start < end; start++) { int temp = array[start]; array[start] = array[end]; array[end] = temp; end--;//迴圈依次,低位右移,高位左移 } for (int i = 0; i < array.length; i++) { System.out.print(array[i] + "\t"); } }
執行結果:

12.求1-100以內所有的質數
/** * 求1-100以內所有的質數 */ public static void getPrimeNumber() { System.out.print("1-100以內的質數有: "); for (int i = 1; i <= 100; i++) { int count = 0; for (int j = 1; j <= i / 2; j++) { if (i % j == 0) { count++; } } if (count < 2) { System.out.print(i + ""); } } }
結果為:

13.輸入一個四位數,要求計算出它的千位,百位,十位,個位,並且打印出來。
/** * 輸入一個四位數,要求計算出它的千位,百位,十位,個位,並且打印出來。 */ public static void printNum() { Scanner scanner = new Scanner(System.in); int num; do { System.out.print("請輸入一個四位數:"); num = scanner.nextInt(); }while (num/1000 < 1); int rest = num; int gewei = rest % 10; rest /= 10; int shiwei = rest % 10; rest /= 10; int baiwei = rest % 10; rest /= 10; int qianwei = rest % 10; System.out.print("個位" + gewei + "十位" + shiwei + "百位" + baiwei + "千位" + qianwei); }
結果為:
