天天看點

【TIJ4】第四章全部習題

第四章 沒啥好說的......

4.1

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題

1 package ex0401;
 2 //[4.1]寫一個程式列印從1到100的值
 3 
 4 public class PrintOneToHundred 
 5 {
 6     public static void main(String[] args)
 7     {
 8         for(int i = 1; i <= 100; ++i)
 9             System.out.print(i + " ");
10     }
11 }      

4.2-4.3

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex040203;
 2 //[4.2]寫一個程式,産生25個int類型的随機數.對于每一個随機值,使用if-else語句來講其分類為大于\小于,或等于緊随它産生的值
 3 //[4.3]修改練習2,把代碼用一個while無限循環包括起來.然後運作它直至鍵盤中斷其運作(通常是通過按Ctrl+C).
 4 
 5 import java.util.Random;
 6 
 7 public class GetRandomInteger 
 8 {
 9     public static void main(String[] args)
10     {
11         int i = 0;
12         Random rand = new Random();
13         int curr = rand.nextInt();
14         int prev = 0;
15         //[4.2]
16         while(++i < 25)
17         //[4.3]
18         //while(true)
19         {
20             prev = rand.nextInt();
21             System.out.print(curr + " ");
22             if(curr > prev)
23                 System.out.print("greater than ");
24             else if(curr < prev)
25                 System.out.print("less than ");
26             else
27                 System.out.print("equals ");
28             curr = prev;
29         }
30         System.out.print(prev);
31     }
32 }      

4.4

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex0404;
 2 //[4.4]寫一個程式,使用兩個嵌套的for循環和取餘操作符來探測和列印素數
 3 
 4 public class GetPrime 
 5 {
 6     public static void main(String[] args)
 7     {
 8         System.out.println(2);
 9         for(int number = 3; number < 10000; number += 2)
10         {
11             int index;
12             for(index = 3; index < number ; index += 2)
13             {
14                 if(number % index == 0)
15                     break;
16             }
17             if(index * index > number)
18                 System.out.println(number);
19         }
20     }
21 }      

4.5

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex0405;
 2 //重複練習[3.10],不要用Integer.toBinaryString()方法,而是用三元操作符合按位操作符來顯示二進制的1和0
 3 
 4 public class AnotherBitOperation 
 5 {
 6     public static void main(String[] args)
 7     {
 8         int first = 0x2AA;
 9         int second =0x133;
10         
11         for(int i = 0; ((first >> i) & 0xFFFF) != 0; ++i)
12             System.out.print((first >> i ) & 1);
13         System.out.println("");
14         for(int i = 0; ((second >> i) & 0xFFFF) != 0; ++i)
15             System.out.print((second >> i ) & 1);
16         System.out.println("");
17         for(int i = 0; (((first & second) >> i) & 0xFFFF) != 0; ++i)
18             System.out.print(((first & second) >> i ) & 1);
19         System.out.println("");
20         for(int i = 0; (((first | second) >> i) & 0xFFFF) != 0; ++i)
21             System.out.print(((first | second) >> i ) & 1);
22         System.out.println("");
23         for(int i = 0; (((first ^ second) >> i) & 0xFFFF) != 0; ++i)
24             System.out.print(((first ^ second) >> i ) & 1);
25         System.out.println("");
26     }
27 }      

4.7

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex0407;
 2 //[4.7]修改本章練習1,通過使用break關鍵詞,使得程式在列印到99時退出.然後嘗試用return達到相同目的
 3 
 4 public class TestBreakAndReturn 
 5 {
 6     public static void main(String[] args)
 7     {
 8         for(int i = 1; i <= 100; ++i)
 9         {
10             if(i == 100)
11                 return;
12                 //break;
13             System.out.print(i + " ");
14         }
15     }
16 }      

4.8

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex0408;
 2 //[4.8]寫一個switch開關語句,為每個case列印一個消息.然後把這個switch放進for循環來測試每個case.
 3 //先讓每個case後面都有break,測試一下會怎麼樣;然後把break删除,看看會怎樣.
 4 
 5 public class TestSwitch 
 6 {
 7     public static void main(String[] args)
 8     {
 9         for(int i = 0; i < 3; ++i)
10         {
11             switch(i)
12             {
13             case 0:
14                 System.out.println("Cat");//break;
15             case 1:
16                 System.out.println("Dog");//break;
17             case 2:
18                 System.out.println("Bird");//break;
19             default:
20                 break;
21             }
22         }
23     }
24 }      

4.9

【TIJ4】第四章全部習題
【TIJ4】第四章全部習題
1 package ex0409;
 2 //[4.9]一個斐波那契數列是由數字從第三個數字其都是前兩個數字的和.
 3 //建立一個方法,接收一個整數參數,并顯示從第一個元素開始,總共有該參數指定的個數所構成的所有斐波那契數字.
 4 
 5 class Fibonacci
 6 {
 7     private static final int FIRST = 1;
 8     private static final int SECOND = 1;
 9 
10     public static void printFibonacci(int length)
11     {
12         if(length <= 0)
13             System.out.println("error!");
14         switch(length)
15         {
16         case 2:
17             System.out.println(FIRST);
18         case 1:
19             System.out.println(SECOND);break;
20         default:
21         {
22             int prevprev = FIRST;
23             int prev = SECOND;
24             int curr;
25             System.out.println(FIRST);
26             System.out.println(SECOND);
27             for(int i = 3; i <= length; ++i)
28             {
29                 curr = prevprev + prev;
30                 System.out.println(curr);
31                 prevprev = prev;
32                 prev = curr;
33             }
34         }
35         }
36     }
37 }
38 public class ShowFibonacci 
39 {
40     public static void main(String[] args)
41     {
42         Fibonacci.printFibonacci(10);
43     }
44 }