1,對基本控制流程的一些練習java
1 package org.base.practice3; 2 3 import org.junit.Test; 4 5 /** 6 * Created with IntelliJ IDEA. 7 * User: cutter.li 8 * Date: 14-3-10 9 * Time: 上午10:14 10 * java基礎練習題第三章 11 */ 12 public class PractiseTest { 13 14 15 @Test 16 public void exercise1() { 17 char x = '你', y = 'e', z = '吃'; 18 19 if (x > 'A') { 20 y = '愛'; 21 z = '情'; 22 } else { 23 y = '我'; 24 } 25 z = '她'; 26 System.out.println(" " + x + y + z); 27 28 } 29 30 @Test 31 public void exercise2() { 32 char c = '\0'; 33 for (int i = 1; i <= 4; i++) { 34 switch (i) { 35 case 1: 36 c = 'b'; 37 System.out.print(c); 38 case 2: 39 c = 'e'; 40 System.out.print(c); 41 break; 42 case 3: 43 c = 'p'; 44 System.out.print(c); 45 default: 46 System.out.print("!"); 47 } 48 } 49 } 50 51 //編寫一個程序求1!+2!+3!+...+10! 52 @Test 53 public void exercise3() { 54 55 int sum = 0; 56 57 for (int i = 1; i <= 10; i++) { 58 int factorial = 1; 59 for (int k = 1; k <= i; k++) { 60 factorial *= k; 61 } 62 System.out.println(i + "的階乘是:" + factorial); 63 sum += factorial; 64 } 65 System.out.println("**********************1到10的階乘之和是:" + sum); 66 } 67 68 //求100之內的素數(素數指在大於1的天然數中,除了1和此整數自身外,沒法被其餘天然數整除的數。) 69 @Test 70 public void exercise4() { 71 72 for (int i = 1; i <= 100; i++) { 73 74 boolean isPrime = true; 75 for (int k = 2; k <= i; k++) { 76 if (i % k == 0 && i != k) { 77 isPrime = false; 78 break; 79 } else { 80 continue; 81 } 82 83 } 84 if (isPrime && i > 1) { 85 System.out.print(i + " , "); 86 } 87 } 88 } 89 90 //分別用do-while和for計算出1+1/2!+1/3!+...的前20項之和 91 @Test 92 public void exercise5() { 93 int i = 1; 94 float sum = 0; 95 do { 96 int factorial = 1; 97 for (int k = 1; k <= i; k++) { 98 factorial *= k; 99 } 100 sum += (float) 1 / factorial; 101 i++; 102 } while (i <= 20); 103 System.out.println("do-while方法計算的前20項的和是:" + sum); 104 105 106 float he = 0; 107 for (int j = 1; j <= 20; j++) { 108 int jiecheng = 1; 109 for (int m = 1; m <= j; m++) { 110 jiecheng *= m; 111 } 112 he += (float) 1 / jiecheng; 113 } 114 System.out.println("for方法計算的前20項的和是:" + he); 115 } 116 117 //求1000之內的完數(一個數若是剛好等於他的除了自己以外的全部因子數之和,這個數稱爲完數) 118 @Test 119 public void exercise6() { 120 121 for (int i = 1; i <= 1000; i++) { 122 int sum = 0; 123 for (int k = 1; k <= i; k++) { 124 if (i % k == 0 && k < i) { 125 sum += k; 126 } 127 } 128 if (sum == i) { 129 System.out.print(i + " , "); 130 } 131 } 132 } 133 134 //分別使用while和for計算出8+88+888+...前10項的和 135 @Test 136 public void exercise7() { 137 138 int sum = 0; 139 140 for (int i = 1; i <= 10; i++) { 141 int num = 0; 142 for (int k = 1; k <= i; k++) { 143 num += Math.pow(10, k - 1); 144 145 } 146 System.out.println("第" + i + "的值是:" + num); 147 sum += 8 * num; 148 } 149 150 System.out.println("前10項的和是:" + sum); 151 } 152 153 //計算出1+2+3+...+n<8888 的最大正整數n 154 @Test 155 public void exercise8() { 156 157 int sum = 0; 158 for (int i = 1; i <= Integer.MAX_VALUE; i++) { 159 sum += i; 160 if (sum >= 8888) { 161 System.out.println("最大的整數是:" + (i - 1)); 162 break; 163 } 164 } 165 166 } 167 168 }
2,對基本類工具Date,Calendar,BigInterger,Math的練習ide
1 package org.base.practice6; 2 3 import org.junit.Test; 4 5 import java.math.BigInteger; 6 import java.text.SimpleDateFormat; 7 import java.util.Calendar; 8 import java.util.Date; 9 10 /** 11 * Created with IntelliJ IDEA. 12 * User: cutter.li 13 * Date: 14-3-10 14 * Time: 下午12:00 15 * java基礎知識第六章練習題 16 */ 17 public class PractiseTest { 18 19 //用date的不帶參數的構造函數建立日期,輸出格式爲 星期 小時 分 秒 20 @Test 21 public void exercise1() { 22 23 Date now = new Date(); 24 String nowStr = new SimpleDateFormat("E HH mm ss").format(now); 25 26 System.out.println("當前的時間:" + nowStr); 27 28 29 } 30 31 //輸入2006年2月的日曆頁面,程序要處理閏年的問題 32 @Test 33 public void exercise2() { 34 35 Calendar calendar = Calendar.getInstance(); 36 calendar.set(2006, 2, 1); 37 38 int year = calendar.get(Calendar.YEAR); 39 System.out.println(year + "年" + Calendar.MONTH + "月"); 40 System.out.println("日 一 二 三 四 五 六"); 41 42 int weeks = calendar.get(Calendar.DAY_OF_WEEK); 43 44 int dayOfMonth = 28; 45 if (year % 400 == 0 || (year % 4 == 0 && year % 100 > 0)) { 46 dayOfMonth = 29; 47 } 48 int week = calendar.get(Calendar.DAY_OF_WEEK); 49 for (int k = 1; k < week; k++) { 50 System.out.print("* "); 51 } 52 for (int i = 1; i <= dayOfMonth; i++) { 53 54 System.out.print(i + " "); 55 if (week % 7 == 0) { 56 System.out.println(); 57 } 58 week++; 59 60 } 61 62 } 63 @Test 64 public void exercise3() { 65 66 Calendar calendar1=Calendar.getInstance(); 67 calendar1.set(1988,1,2); 68 69 Calendar calendar2=Calendar.getInstance(); 70 calendar2.setTime(new Date()); 71 72 long days= calendar2.getTimeInMillis()-calendar1.getTimeInMillis(); 73 74 System.out.println("我活了"+days/(24*60*60*1000)+"天"); 75 76 77 } 78 79 @Test 80 public void exercise4() { 81 System.out.println(Math.nextUp(100f)); 82 } 83 @Test 84 public void exercise5() { 85 86 BigInteger sum = BigInteger.ZERO; 87 for (BigInteger i = new BigInteger("1", 10); i.intValue() <= 30; i=i.add(new BigInteger("1"))) { 88 BigInteger factorial = new BigInteger("1"); 89 for (BigInteger k = new BigInteger("1"); k.intValue() <= i.intValue();k= k.add(new BigInteger("1"))) { 90 factorial = factorial.multiply(k); 91 } 92 System.out.println(i+"的階乘是:" + factorial); 93 sum = sum.add(factorial); 94 } 95 System.out.println("1!+2!+...+30!的和是:" + sum); 96 97 98 } 99 100 }
3,對字符串的一些練習函數
1 package org.base.practice5; 2 3 import org.junit.Test; 4 5 import java.util.Arrays; 6 7 /** 8 * Created with IntelliJ IDEA. 9 * User: cutter.li 10 * Date: 14-3-10 11 * Time: 下午2:33 12 * 字符串相關練習題 13 */ 14 public class PractiseTest { 15 16 @Test 17 public void exercise1() 18 { 19 String str="I am a 7Road employee !"; 20 21 System.out.println(str); 22 23 System.out.println(str.toLowerCase()); 24 25 System.out.println(str.toUpperCase()); 26 27 } 28 29 @Test 30 public void exercise2() 31 { 32 String str="cutter GG :".concat("do u want to find a job ?"); 33 34 System.out.println(str); 35 36 System.out.println(29&51+((29^51)>>1)); 37 38 39 } 40 41 @Test 42 public void exercise3() 43 { 44 String str="中國科學技術大學"; 45 char a=str.charAt(2),b=str.charAt(6); 46 47 System.out.println(a+" , "+b); 48 } 49 50 @Test 51 public void exercise4() 52 { 53 int a[]={465,2,7979,12,9,3,9655,-10}; 54 55 Arrays.sort(a); 56 57 for(int i:a) 58 { 59 System.out.print(i+" , "); 60 } 61 62 System.out.println(); 63 int b[]= Arrays.copyOf(a,20); 64 65 for(int i:b) 66 { 67 System.out.print(i + " , "); 68 } 69 } 70 71 72 @Test 73 public void exercise5() 74 { 75 Object[] a=new Object[10]; 76 System.arraycopy(new Object[]{"a",0,"cdef",'f'},0,a,0,2); 77 78 for(Object i:a) 79 { 80 System.out.print(i + " , "); 81 } 82 } 83 }
以上練習使用的是junit4.11,爲求簡單,使用的是命令行輸出,爲了複習基本的java知識,特找了java2使用教程的練習來練手,看一遍書,溫故而知新···工具