Java超簡明入門學習筆記(二)


Java編程思想第4版學習筆記(二)
第三章 操作符 & 第四章 控制執行流程(流程控制語句)
 
      第三章和第四章的內容主要是講操作符和流程控制語句,Java的大多數操作符和流程控制語句都和C/C++的十分類似,因此把這兩章內容匯成一章,挑出Java獨特的地方進行學習。
 
      第三章
 
      知識點1 :P39,3.2,操作符,優先級
      Java操作符和其他語言一樣,作用於操作數,產生新值。各個操作符的優先級和結合性類似C/C++。
      這里有一些特殊的地方:
      + 操作符可以用於字符串,把字符串和其他對象連接在一起,比如String s = new String(); int i = 0;System.out.println(s+i+1.2); 這里+操作符發現自己的左操作數是String對象,右操作數不是,因此就會把右操作數轉化為一個臨時的String對象然后再和左操作數相加。
      = 賦值操作符可以使左操作數 的對象引用 成為右操作數所引用對象的別名。 String s = new String("123");  String s2 = new String("456"); s = s2; 之后,s和s2所引用的對象的值都會變成"456"。這一點也同樣體現在函數的參數傳遞上。
      == 判等關系操作符也是比較特殊的,當它的左右操作數都是對象引用的時候,它判斷的是這兩個引用是否引用了同一個對象,而不是它們引用的對象值是否相同。如果要想判斷兩個對象引用所引用的值是否相同,需要使用類中的equals成員函數。如果沒有這個成員函數,你需要自己創造一個。基本類型則無此限制。
      >>>無符號右移位運算符,比起右移運算符,無論操作的是數是否是正數,它每次都往左側填0。
      類型轉換運算符,用法類似這樣,(想轉換成的類型)變量/對象/字面值。有些類型之間不能夠互相轉換,不過除了boolean,各個基本類型之間都能互相轉換。也可以通過這種方式,把高精度類型的數轉成低精度的類型,這種轉換成為“窄化轉換”。
      類型提升。char,byte,short類型的值在參加計算時,會自動地轉換成int類型,再進行計算。兩個不同的基本類型做算術運算時,精度低的那個類型的值會自動提升為精度高的類型。
       
       知識點2 :第三章提到的一些類庫和方法
     類名:Random
     所屬包:java.util
     方法一:構造函數,參數為種子,可以為空,如果為空則用當前時間做種子。
     方法二:nextInt(),參數可以為空,也可以是一個整數,如有參數,這個函數將生成范圍在[0~參數值)之間的隨機數。如果參數為空,則無限制。類似地,還有nextFloat()nextLong()nextDouble()等方法。
 
      方法:toBinaryString()
     所屬包:java.lang
      所屬類:Integer, Double等等基本類型的包裝器類型中。
      參數:toBinaryString屬於哪個類,調用時就需要哪個類的對象作為參數。
      返回值:String
      作用:是一個靜態函數成員,用於獲得一個數值的二進制表示(字符串形式)。
 
      字段:E
      所屬包:java.lang
      所屬類:Math
      介紹:是一個代表自然對數的基數的double類型靜態常量。
 
     方法:round()
     所屬包:java.lang
      所屬類:Math
      參數:float或者 double
      返回值:int(參數為float時)或long(參數為double時)。
      作用:返回給定浮點數四舍五入之后的整數結果。
     
     第四章
 
      基本流程控制語句
      選擇分支:if-else,switch
      循環語句:do..while,while,for,range for
      跳出循環或switch內部:break
      跳出單次循環:continue
      
      第四章提到的一些其他的相關Java語法
     靜態方法Math.random();
      可以產生0~1之間的隨機值。
      標簽
      一個標簽形如:標簽名:,比如label1:,只能在迭代語句之前使用,標簽后面只能接空白或者迭代語句或者注釋。在循環內部使用break 標簽名;即可跳轉到標簽處並且不再進入剛跳出的循環。使用continue 標簽名;,即可跳轉到標簽處,繼續進行循環迭代。經常被用於想要馬上跳出多層循環的情況。
      二進制數字字面值
      Java SE7中,可以在二進制字面值前面加0b代表一個二進制數值,比如int a  = 0b10101101;
 
  第三章 練習題
     練習1:練習使用打印語句,略。
     練習2:創建一個包含一個float域(字段)的類,並用這個類來展示別名機制。
 1 class Test{
 2     float i;
 3 }
 4 
 5 public class MainTest {
 6     public static void main(String[] args){
 7         Test t1 = new Test();
 8         Test t2 = new Test();
 9 
10         t1.i = 111;
11         t2.i = 222;
12 
13         System.out.println("ti.i = " + t1.i);
14         System.out.println("t2.i = " + t2.i);
15 
16         t1 = t2;
17 
18         System.out.println("ti.i = " + t1.i);
19         System.out.println("t2.i = " + t2.i);
20 
21         t1.i = 233;
22         System.out.println("ti.i = " + t1.i);
23         System.out.println("t2.i = " + t2.i);
24     }
25 }
練習2答案
     練習3: 創建一個包含一個float域(字段)的類,並用這個類來展示方法調用時(參數傳遞時)的別名機制。
 1 class Test{
 2     float i;
 3 }
 4 
 5 class FuncTest{
 6     static void func(Test t){
 7         t.i = 2.17F;
 8     }
 9 }
10 
11 public class MainTest {
12     public static void main(String[] args){
13         Test t = new Test();
14         System.out.println("t.i = " + t.i);
15         FuncTest.func(t);
16         System.out.println("t.i = " + t.i);
17     }
18 }
練習3答案
     練習4: 編寫一個計算速度的程序,它所使用的距離和時間都是常量。
 1 class Func{
 2     static double speedNeeded(double length,double time){
 3         return length/time;
 4     }
 5 }
 6 
 7 public class MainTest {
 8     public static void main(String[] args){
 9         Func.speedNeeded(23.8,14.5);
10     }
11 }
練習4答案
     練習5、6: 創建一個名為Dog的類,它包含兩個String域:name和says。在main方法中,創建兩個不同的Dog類對象,一個名為spot,叫聲Ruff!;另一個名為scruffy,叫聲Wurf!,然后顯示這兩個對象的名字和叫聲。
     接着,創建一個新的Dog類引用,並對其賦值為spot,分別使用==和equals比較這個引用和spot引用,查看結果。
 1 class Dog{
 2     String name;
 3     String says;
 4 
 5     void shows(){
 6         System.out.println("name: "+name+" Says: "+says);
 7     }
 8 }
 9 
10 public class MainTest {
11     public static void main(String[] args){
12         Dog spot = new Dog();
13         spot.name = "spot";
14         spot.says = "Ruff!";
15 
16         Dog scruffy = new Dog();
17         scruffy.name = "scruffy";
18         scruffy.says = "Wurf!";
19 
20         spot.shows();
21         scruffy.shows();
22 
23         Dog newDog = spot;
24         System.out.println(newDog==spot);
25         System.out.println(newDog.equals(spot));
26     }
27 }
練習5、6答案
     練習7: 編寫一個程序,模擬扔硬幣的結果。
 1 import java.util.*;
 2 
 3 class Coin{
 4     static void test(){
 5         Random r = new Random();
 6         System.out.println(r.nextInt(2)==1?"正面":"反面");
 7     }
 8 }
 9 
10 public class MainTest {
11     public static void main(String[] args){
12         Coin.test();
13     }
14 }
練習7答案
     練習8: 展示用16進制和8進制記數法(字面值)來操作long值(賦值),並顯示其二進制結果。
1 public class MainTest {
2     public static void main(String[] args){
3         long l1 = 0777;
4         long l2 = 0xC2B;
5 
6         System.out.println(Long.toBinaryString(l1));
7         System.out.println(Long.toBinaryString(l2));
8     }
9 }
練習8答案
     練習9:多種解,略。
     練習10:編寫一個具有兩個常量值的程序,一個具有交替的二進制1和0,最低有效位是0,另一個也 具有交替的二進制1和0,最低有效位是1。取這兩個值,用按位運算符以所有可能的方式結合運算它們,然后使用二進制形式表示出來。
 1 public class MainTest {
 2     public static void main(String[] args){
 3         int number = 0;
 4         for(int i =0 ;i<32;i+=2){
 5             number|=1;
 6             number<<=2;
 7         }
 8         number|=1;
 9 
10         int number2 = 0;
11         number2=~number;
12 
13         int r1 = number^number2;
14         int r2 = number|number2;
15         int r3 = number&number2;
16 
17         System.out.println(Integer.toBinaryString(number));
18         System.out.println(Integer.toBinaryString(number2));
19     }
20 }
練習10答案
     練習11:以一個最高位為1的二進制數字開始,用有符號的右移操作符對其進行右移,直至所有的二進制數都被移出為止,顯示每一次移動的二進制結果。
 1 public class MainTest {
 2     public static void main(String[] args){
 3         int number = 0b1111111111111111111111111111111;
 4 
 5         while(number!=0)
 6         {
 7             number>>=1;
 8             System.out.println(Integer.toBinaryString(number));
 9         }
10 
11         System.out.println(number);
12     }
13 }
練習11答案
     練習12:以一個所有 位都為1的二進制數字開始,先左移它,然后用無符號的右移操作符對其進行右移,直至所有的二進制數都被移出為止,顯示每一次移動的二進制結果。
 1 public class MainTest {
 2     public static void main(String[] args){
 3         int number = 0b1111111111111111111111111111111;
 4 
 5         number<<=1;
 6         System.out.println(Integer.toBinaryString(number));
 7 
 8         while(number!=0)
 9         {
10             number>>>=1;
11             System.out.println(Integer.toBinaryString(number));
12         }
13 
14         System.out.println(number);
15     }
16 }
練習12答案
     練習13:編寫一個方法,它以二進制方式顯示char類型的值,使用多個不同的字符來展示它。
 1 class CharShows{
 2     static void show(char c){
 3         int number = c;
 4         System.out.println(Integer.toBinaryString(number));
 5     }
 6 }
 7 
 8 public class MainTest {
 9     public static void main(String[] args){
10         CharShows.show('a');
11         CharShows.show('z');
12         CharShows.show('0');
13         CharShows.show('9');
14     }
15 }
練習13答案
     練習14:編寫一個接收兩個字符串參數的方法,用各種布爾值的比較關系來比較這兩個不同的字符串,然后把結果打印出來,做!和!=比較和同時時,用equals做測試。在main()中用幾個不同的字符串對象調用這個方法。
 1 class Test{
 2     static void comp(String s1, String s2){
 3         System.out.println( "s1 == s2 : " + (s1==s2?"true":"false"));
 4         System.out.println("s1 != s2 : " + (s1!=s2?"true":"false"));
 5         System.out.println("s1.equals(s2) : " + (s1.equals(s2)?"true":"false"));
 6     }
 7 }
 8 
 9 public class MainTest {
10     public static void main(String[] args){
11         String s1 = new String("1234");
12         String s2 = new String("12345");
13 
14         Test.comp(s1,s2);
15     }
16 }
練習14答案
 
  第四章 練習題
     練習1: 打印1~100的值。
1 public class MainTest {
2     public static void main(String[] args){
3         for(int i = 1;i<=100;++i){
4             System.out.println(i);
5         }
6     }
7 }
練習1答案
     練習2:產生25個int類型的隨機數,對於每一個非末一個的隨機值,使用if-else語句把它和緊隨它生成的隨機值對比,輸出結果:是大於,小於,還是 等於。
 1 import java.util.*;
 2 
 3 public class MainTest {
 4     public static void main(String[] args){
 5         int beforeNum = 0;
 6         Random r = new Random();
 7         for(int i = 0;i<25;++i){
 8             int thisNum = r.nextInt(100);
 9             System.out.println("new Number is : " + thisNum);
10             if(i!=0){
11                 if(beforeNum>thisNum){
12                     System.out.println("Before Number : "+beforeNum+" > thisNum : "+thisNum);
13                 }
14                 else if(beforeNum<thisNum){
15                     System.out.println("Before Number : "+beforeNum+" < thisNum : "+thisNum);
16                 }
17                 else{
18                     System.out.println("Before Number : "+beforeNum+" == thisNum : "+thisNum);
19                 }
20             }
21             beforeNum = thisNum;
22         }
23     }
24 }
練習2答案
     練習3:和環境有關的行為,略。
     練習4: 寫一個程序,給出一個整數,判斷它是否為素數(用for和%)。
 1 class PrimeNumber{
 2     static boolean isPrimeNumber(int number){
 3         boolean flag = true;
 4         if(number==2||number==3)return true;
 5         else{
 6             for(int i = 2;i<=Math.sqrt(number)+1;++i){
 7                 if(number%i==0)
 8                 {
 9                     flag = false;
10                     break;
11                 }
12             }
13         }
14 
15         return flag;
16     }
17 }
18 
19 public class MainTest {
20     public static void main(String[] args){
21         System.out.println("2 "+(PrimeNumber.isPrimeNumber(2)?"is a PN":"is Not a PN"));
22         System.out.println("3 "+(PrimeNumber.isPrimeNumber(3)?"is a PN":"is Not a PN"));
23         System.out.println("4 "+(PrimeNumber.isPrimeNumber(4)?"is a PN":"is Not a PN"));
24         System.out.println("8 "+(PrimeNumber.isPrimeNumber(8)?"is a PN":"is Not a PN"));
25         System.out.println("17 "+(PrimeNumber.isPrimeNumber(17)?"is a PN":"is Not a PN"));
26         System.out.println("24 "+(PrimeNumber.isPrimeNumber(24)?"is a PN":"is Not a PN"));
27     }
28 }
練習4答案
      練習5: 多種解,略。
      練習6:修改本章書上兩個含有test方法的例子,讓它們接受額外的兩個參數begin和end,在測試testval時先判斷它是否在begin和end之間(含begin和end)。
 1 class IfElse1{
 2     static int result = 0;
 3     static void test(int testval,int target,int begin,int end){
 4         if(testval>=begin&&testval<=end){
 5             if(testval>target){
 6                 result = 1;
 7             }
 8             else if(testval<target){
 9                 result = -1;
10             }
11             else result = 0;
12         }
13     }
14 }
15 
16 class IfElse2{
17     static int test(int testval,int target,int begin,int end){
18         if(testval>=begin&&testval<=end){
19             if(testval>target){
20                 return 1;
21             }
22             else if(testval<target){
23                 return -1;
24             }
25             else return 0;
26         }
27         return -1;
28     }
29 }
練習6答案
      練習7:修改本章練習1,通過使用break關鍵詞(或者return關鍵詞),使其只輸出范圍為1~99的值。
 1 public class MainTest {
 2     public static void main(String[] args){
 3         for(int i = 1;i<=100;++i){
 4             System.out.println(i);
 5             if(i==99){
 6                 break;  //return
 7             }
 8         }
 9     }
10 }
練習7答案
     練習8:寫一個switch語句,為每個case打印一個消息。然后把這個switch語句放進for循環里,來測試每一個case。並測試case后面帶break和不帶break的差異。
 1 public class MainTest {
 2     public static void main(String[] args){
 3         for(int i = 0;i<3;++i){
 4             switch (i){
 5                 case 0:
 6                     System.out.println("0");
 7                     //break;
 8 
 9                 case 1:
10                     System.out.println("1");
11                     //break;
12 
13                 case 2:
14                     System.out.println("2");
15                     //break;
16             }
17         }
18     }
19 }
練習8答案
     練習9:創建一個方法,接受一個整數參數,並顯示從1~指定整數中的斐波那契數字,比如參數是5,則輸出1、1、2、3、5。
 1 class Feb{
 2     static void feb(int max){
 3         int num1 = 1;
 4         int num2 = 1;
 5         int newNum = 0;
 6 
 7         System.out.println(1);
 8         System.out.println(1);
 9 
10         while(max>=num1+num2){
11             newNum = num1+num2;
12             num1 = num2;
13             num2 = newNum;
14             System.out.println(num2);
15         }
16     }
17 }
18 
19 public class MainTest {
20     public static void main(String[] args){
21         Feb.feb(5);
22     }
23 }
練習9答案
     練習10:吸血鬼數字是指位數為偶數的數字,可以由一堆數字相乘得到,而這對數字各包含乘積一半位數的數字,其中從最初的數字中選取的數字可以任意排列。以兩個0結尾的數字不屬於吸血鬼數字。例如,1260=21*60,1827=21*87,2187=27*81。因此,1260、1827、2187都是吸血鬼數字。寫一個程序,找出四位數的所有吸血鬼數字。
 1 import java.util.*;
 2 
 3 public class MainTest {
 4     public static void main(String[] args) {
 5         method();
 6     }
 7 
 8     private static void method() {
 9         int[] startDigit = new int[4];
10         int[] productDigit = new int[4];
11         for (int num1 = 10; num1 <= 99; num1++)
12             for (int num2 = num1; num2 <= 99; num2++) {
13                 if ((num1 * num2) % 9 != (num1 + num2) % 9)
14                     continue;
15                 int product = num1 * num2;
16                 startDigit[0] = num1 / 10;
17                 startDigit[1] = num1 % 10;
18                 startDigit[2] = num2 / 10;
19                 startDigit[3] = num2 % 10;
20                 productDigit[0] = product / 1000;
21                 productDigit[1] = (product % 1000) / 100;
22                 productDigit[2] = product % 1000 % 100 / 10;
23                 productDigit[3] = product % 1000 % 100 % 10;
24                 int count = 0;
25                 for (int x = 0; x < 4; x++)
26                     for (int y = 0; y < 4; y++) {
27                         if (productDigit[x] == startDigit[y]) {
28                             count++;
29                             productDigit[x] = -1;
30                             startDigit[y] = -2;
31                             if (count == 4)
32                                 System.out.println(num1 + " * " + num2 + " : "+ product);
33                         }
34                     }
35             }
36     }
37 }
練習10答案


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM