練習1:寫一個程序,打印從1到100的值
public class Print1To100{ public static void main(String args[]){ for(int i = 1 ; i <= 100 ; i++){ System.out.println("value:" + i) ; } } }
練習2:寫一個程序,產生25個int類型的隨機數。對於每個隨機值,使用if-else語句來將其分類為大於、小於或等於緊隨它而隨機生成的值。
1 public class RandomNumber{ 2 public static void main(String args[]){ 3 int i = 0 ; 4 Random rand =new Random() ;//實例化Random() 5 int num1 = 0 ; 6 while(i<25){ 7 int num2 = rand.nextInt(100) ;//產生一個100以內的整數 8 System.out.print(num2) ; 9 if(num1 == num2){//比較新生成的數值是否和前一個數值是否相等 10 System.out.println(" = The previous number") ; 11 } 12 else if(num1 < num2){//比較新生成的數值是否大於前一個數值 13 System.out.println(" > The previous number") ; 14 } 15 else{比較新生成的數值是否小於前一個數值 16 System.out.println(" < The previous number") ; 17 } 18 19 num1 = num2 ; 20 i++ ; 21 22 } 23 } 24 }
練習3:修改練習2,把代碼用一個while無線循環包括起來。然后運行它直至用鍵盤中斷其運行(Ctrl+C)。
1 public class RandomNumber{ 2 public static void main(String args[]){ 3 int i = 0 ; 4 Random rand =new Random() ;//實例化Random() 5 int num1 = 0 ; 6 while(true){//終止條件修改為true 7 int num2 = rand.nextInt(100) ;//產生一個100以內的整數 8 System.out.print(num2) ; 9 if(num1 == num2){//比較新生成的數值是否和前一個數值是否相等 10 System.out.println(" = The previous number") ; 11 } 12 else if(num1 < num2){//比較新生成的數值是否大於前一個數值 13 System.out.println(" > The previous number") ; 14 } 15 else{比較新生成的數值是否小於前一個數值 16 System.out.println(" < The previous number") ; 17 } 18 19 num1 = num2 ; 20 i++ ; 21 22 } 23 } 24 }
練習4:寫一個程序,使用兩個嵌套for循環和取余操作符(%)來探測和打印素數。
1 public class PrimeNumber{ 2 static boolean isPrimeNum(int num){//判斷是否是素數 3 for(int i = 2; i < Math.abs(num) ; i++){ 4 if(num%i == 0){ 5 return false ;//不是素數返回false 6 } 7 } 8 return true ;//是素數返回true 9 } 10 public static void main(String args[]){ 11 for(int i = -100 ; i < 1000 ; i++){ 12 if(isPrimeNum(i)){//調用isPrimeNum()方法 13 System.out.println(i) ; 14 } 15 } 16 } 17 }
練習5:重復第三章練習10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符來進行顯示二進制的1和0.
這一題沒有做。。。
練習6:修改前兩個程序中的兩個test()方法,讓他們接受兩個額外的參數begin和end,這樣在測試testval時將判斷它是否在begin和end之間的范圍(包含begin和end)。
練習7:修改本章練習1,通過使用break關鍵詞,使得程序在打印99時退出。然后嘗試使用return來達到相同的目的。
1 public class Print1To100{ 2 public static void main(String args[]){ 3 for(int i = 1 ; i <= 100 ; i++){ 4 if(i == 99){ 5 break ; 6 } 7 System.out.println("value:" + i) ; 8 } 9 } 10 }
練習8:寫一個switch開關語句,為每個case打印一條消息。然后把這個switch放進for循環來測試每個case。先讓每個case后面都有break,然后把break刪除測試。
1 public class SwitchTest{ 2 public static void main(String[] args) { 3 for(int i = 0 ; i < 10 ; i++){ 4 switch(i){ 5 case 1:System.out.println("case 1") ; 6 break ; 7 case 2:System.out.println("case 2") ; 8 break ; 9 case 3:System.out.println("case 3") ; 10 break ; 11 case 4:System.out.println("case 4") ; 12 break ; 13 case 5:System.out.println("case 5") ; 14 break ; 15 case 6:System.out.println("case 6") ; 16 break ; 17 default:System.out.println("default") ; 18 } 19 } 20 } 21 }
練習9:一個斐波那契數列由數字1、1、2、3、5、8、13、21、34等等組成的,其中每個數字(第三個數字起)都是前兩個數字的之和。
1 public class Fibonacci{ 2 public static void main(String[] args) { 3 int len = Integer.parseInt(args[0]);//取出參數 4 int num1 = 1 ;//初始化變量 5 int num2 = 1 ; 6 int num3 = 0 ; 7 for(int i = 2 ; i < len ; i++){//循環打印出指定的長度 8 num3 = num1 + num2 ;//前兩個數相加等於等三個數 9 num1 = num2 ; //交換數值 10 num2 = num3 ; 11 System.out.print(num3 + ",") ; 12 } 13 } 14 }
練習10:吸血鬼數字是指位數為偶數的數字,可以有一對數字相乘而得到,而這對數字各包含乘積的一半位數的數字。以兩個0結尾的數字是不允許的,例如一下吸血鬼數字:
1260 = 21 * 60
1827 = 21 * 87
寫一個程序找出4位數所有的吸血鬼數字。
1 public class Vampire{ 2 public static void main(String[] args) { 3 String[] ar_str1 , ar_str2 ; 4 for(int num = 1000 ; num <= 10000 ; num++){//設定被除數為4位數 5 for(int divisor = 10 ; divisor < 100 ; divisor++){//設定除數為兩位數 6 int num1 = num / divisor ; 7 int remainder = num % divisor ;//余數 8 int remainder1 = divisor % 10 ; 9 int remainder2 = num1 % 10 ; 10 //判斷是否為吸血鬼數字 11 if(remainder == 0 && (remainder1 != 0 || remainder2 != 0)){//判斷是否能夠被整除,且兩個除數末尾不全為0 12 ar_str1 = String.valueOf(num).split("") ;//把num拆分成單個字符 13 ar_str2 = (String.valueOf(num1) + String.valueOf(divisor)).split("") ;//把num1和divisor拆分成字符 14 java.util.Arrays.sort(ar_str1) ;//num排序 15 java.util.Arrays.sort(ar_str2) ; 16 if(java.util.Arrays.equals(ar_str1, ar_str2)){//比較num是否和num1+divisor相同,如果相同打印,並終止內嵌的循環 17 System.out.println(num + "=" + divisor + "*" + num1) ; 18 break ; 19 } 20 } 21 } 22 } 23 } 24 }