-Scanner實例,猜數字。html
package src.week1; import java.util.Scanner; public class g { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);//創建scanner實例 int num=(int)(Math.random()*10); int guess; do { System.out.print("猜數字(0-9):"); guess=scanner.nextInt(); }while (guess!=num); System.out.println("year!!"); } }
代碼運行結果以下圖所示
Scanner的NextInt()方法會看看標準輸入中有無輸入下一個字符串,有的話將至剖析爲int型。
-使用java.math.BigDecimaljava
package src.week1; import java.math.BigDecimal; public class h { public static void main(String[] args) { BigDecimal op1=new BigDecimal("0.1"); BigDecimal op2=new BigDecimal("0.1"); BigDecimal op3=new BigDecimal("0.1"); BigDecimal result=new BigDecimal("0.8"); if(op1.add(op2).add(op3).equals(result)) { System.out.printf("==0.3"); } else System.out.println("!=0.3"); } }
java遵照浮點數運算規範,對於0.1會無限循環下去,沒法精確,故可使用java.math.BigDecimal方法,在建立時會剖析傳入字符串,以默認精度進行接下來的運算。git
package src.week2; public class g { public static void main(String[] args) { int[][] cords={ {1,2,3}, {4,5,6} };//數組初始化 int[][] cords1=new int[][] { {1,2,3}, {4,5,6} };//數組也能夠這樣初始化 for(int x=0;x<cords.length;x++)//數組有幾行 { for(int y=0;y<cords[x].length;y++)//每行有幾個元素 { System.out.printf("%2d",cords[x][y]); }System.out.println(); } for (int[] hang:cords)//數組有幾行 { for(int lie:hang)//每行有幾個元素 System.out.printf("%2d",lie); } } }
結果以下圖所示:
上述代碼中,hang參考到的對象就是一維數組對象,外層for循環就是循環取得cords參考對象的每一個索引,將參考到的對象指定給int[]類型的hang名稱。cords.length的值就是問cords參考對象索引0de參考對象的長度是什麼,答案是3.編程
Integer[][] cords=new Integer[3][2];
package src.week1; import java.util.Arrays; public class d { public static void main(String[] args) { int scores1[]={1,2,3,4,5,6,7,8,9}; int scores2[]=Arrays.copyOf(scores1,scores1.length); for(int a:scores2) { System.out.printf("%3d",a); } System.out.println(); scores2[0]=99; for (int x:scores1) { System.out.printf("%3d",x); } } }
執行結果以下圖所示:數組
package src.week1; class clothes{ String color; char size; clothes(String color,char size) { this.color=color; this.size=size; } } public class d { public static void main(String[] args) { clothes[] c1={new clothes("red",'l'),new clothes("red",'l')}; clothes[]c2=new clothes[c1.length]; for(int i=0;i<c1.length;i++) { c2[i]=c1[i]; } c1[0].color="yellow"; System.out.println(c2[0].color); } }
for(int i=0;i<c1.length;i++) { clothes c=new clothes(c1[i].color,c1[i].size);//自行復制元素 c2[i]=c; }
結果一樣也是「yellow」,但這種複製被叫作淺層複製,僅僅是將c1中的每一個參考對象也給c2每一個索引來參考。至關於c1,c2索引的對象都是同一個。而深層複製行爲也就是說,將c1每一個索引參考的對象被複制後分別指定給c2的每一個索引,即索引的參考對象相同但不是同一個。dom
package src.week2; public class h { public static void main(String[] args) { String a1="hello"; String a2="hello"; String a3=new String("hello"); String a4=new String("hello"); System.out.println(a1==a2); System.out.println(a3==a2); System.out.println(a3==a4); System.out.println(a1.equals(a2)); System.out.println(a2.equals(a3)); System.out.println(a3.equals(a4)); } }
代碼執行結果以下:在java中,以「」包括的字符串,只要內容徹底相同,不管其出現幾回,JVM都只會創建一個String實例,並在字符串池中進行維護。「==」用於指向對象徹底相同兩個實例纔會出現true。而在上述代碼中,a3和a4分別參考至新建的String實例。故獲得的結果是false。但使用equals(),就能夠獲得不一樣的答案,由於其強調的是比較對象內容是否相同。函數
package src.week2; public class h { public static void main(String[] args) { String a="adfaads"; System.out.println(a); System.out.println(a.length());//取得字符串長度 System.out.println(a.charAt(2));//取得字符串中第2個字符 System.out.println(a.toUpperCase());//將字符串中的全部字符大寫 char[] cs={'a','a','d','f'}; String b=new String(cs); char[]cs2=a.toCharArray();//將字符串以數組形式返回 System.out.println("你的名字:" +a);// } }
執行結果以下學習
方法 | 說明 |
---|---|
Byte.parseByte(a); | 將a剖析成byte整數 |
Short.parseShort(a); | 將a剖析成Short整數 |
Long.parseLong(a); | 將a剖析long整數 |
Integer.parseInt(a); | 將a剖析成Integer整數 |
Float.parseFloat(a); | 將a剖析成float整數 |
Double.parseDouble(a); | 將a剖析成double整數 |
package src.week2; class cashcard { String number; int balance; int bonus; cashcard(String number,int balance,int bonus) { this.number=number; this.balance=balance; this.bonus=bonus; } }public class h { public static void main(String[] args) { cashcard[] a = { new cashcard("444", 455, 1), new cashcard("434", 455, 1), new cashcard("424", 455, 1), }; for (cashcard cash : a) { System.out.printf("(%s,%d,%d)%n", cash.number, cash.balance, cash.bonus); } } }
用java的構造語法,實現了對向初始化的封裝,this
package src.week2; import java.util.Scanner; class cashcard { String number; int balance; int bounce; cashcard(String number,int balance,int bounce) { this.number=number; this.balance=balance; this.bounce=bounce; }; void store (int money) { if(money>0) { this.number+=money; if(money>=1000) { this.bounce++; } }else { System.out.println("儲值不能爲負"); } } void charge(int money) {if(money>0) { if(money <=this.balance) { this.balance-=money; } else System.out.printf("錢不夠了"); } else System.out.printf("取值不能爲負"); } int exchange(int bounce) { if(bounce>0){ this.bounce-=bounce; } return bounce; } } public class h { public static void main(String[] args) { Scanner a=new Scanner(System.in); int money,bounce; money=a.nextInt(); bounce=a.nextInt(); cashcard a1=new cashcard("a111",100,5); a1.store(money); cashcard a2=new cashcard("a111",100,5); a2.charge(money); cashcard a3=new cashcard("a111",100,5); a3.exchange(bounce); } }
若是不用返回值,方法名稱前能夠用void。創建cashcard的方法以下.net
cashcard [] a= { new cashcard("a44",12,2), .... }
cashcard a1=new cashcard("a111",123,1); cashcard a2=new cashcard("a111",123,1); cashcard a3=new cashcard("a111",123,1);
兩者的前提是:
class cashcard { String number; int balance; int bounce; cashcard(String number,int balance,int bounce) { this.number=number; this.balance=balance; this.bounce=bounce; };
class cashcard2 { private String number; private int balance; private int bounce;//使用private定義私有成員 }
int getBalance() { return balance; } int getBounce() { return bounce; } String getNumber() { return number;//提供取值方法成員; }
此時咱們封裝了類私有數據,讓用戶沒法直接存取,必須經過我所提供的方法,即要想調用private定義過的值,必須使用同一個包內的方法,而且用戶也不會知道對象的內部細節。取值方法,形如getBalance(),等取值方法。
package src.week2; public class p {//這是一個公開類; public static void main(String[] args) {//main()方法 } public p(String number, int balance, int bounce)//這是公用構造函數; { } public static void main(int money)//公開方法; { } public int charge()//公開方法; { return 4; } }
聲明類加上public後表示它是個公開類,其餘包能夠直接使用,在構造函數上聲明public,表示其餘包的類能夠直接調用這個構造函數。在方法中聲明public,表示其餘包的方法中能夠直接調用這個方法。一樣也能夠在數據成員上聲明public。
main()方法是java應用程序的入口方法,沒有這個方法,java程序沒法編譯。這個方法必須是public static void類型的。方法必須接受一個字符串數組的參數等等。
-構造函數是與類名稱同名,無需聲明返回類型的方法。建立對象時,數據成員會初始化,若是沒有指定初始值,會使用默認值初始化,參考P133表5.1。若是定義類時沒有撰寫任何構造函數,會自動加入默認構造函數,因此在沒有撰寫任何構造函數時,也能夠這樣以無自變量方式構造函數:
Some a=new Some();
注意:自行撰寫的無參數無內容的構造函數不能成爲默認構造函數。
能夠定義多個構造函數,只要參數類型或者個數不一樣。可用意志的名稱來調用相似功能的方法,方法重裁可根據傳遞自變量的類型不一樣,也可根據參數列個數的不一樣來設計方法重裁。注意:返回值類型不一樣不可做爲方法重裁的依據。
package src.week2; public class p {//這是一個公開類; public static void main(String[] args) {//main()方法 } public int some(int i) { return 0; } public int some(int i) { return 0.0; }
以上編譯出現兩個錯誤:1.重複定義 2.返回值類型出錯。
在對象創建後爲「這個對象」的參考名稱。在構造函數參數與對象數據同名時,能夠用this加以區別。
package src.week2; public class p{ public static void main(String[] args) { } private int a=10; private String text="n.a."; public p(int a) { if(a>0) { this.a=a; } } public p(int a,String text) { this(a); if(text!=null) { this.text=text; } } }
this()表明了調用另外一個構造函數,至於調用哪一個構造函數,則視調用this()時給的自變量類型與個數而定,能夠避免程序中出現重複的語句。同時可使用可使用this()來進行調用函數,至於調用的是哪一個函數則視調用this()時給的自變量類型與個數而定。
package src.week2; public class p { final int x;//編譯出錯 public static void main(String[] args) { } p() { }//若是調用這個函數,x就沒有被賦值。編譯出錯 p(int c,int b) { this(10);//至關於引用了p(int 10)即下面這個函數對final對象成員x設值 } p(int x){ this.x=x;//對final對象成員x設值 } }
class ball{ double a; static final double PI=3.1459; }
就能夠這樣取得圓周率:
System.out.println(ball.PI);
注意:遵照命名習慣;static方法或者區塊中不能出現this關鍵字。由於static成員屬於類,而不是個別對象。
package src.week2; class ball { double a; static void a() { double i = this.a; } void c() { } static void v() { c();//隱含使用了this.c() } }
就會出現編譯錯誤。
-注意:int...聲明的變量實際上展開爲數組;在使用不定長度自變量時,必須是參數列中的最後一個,使用兩個以上不定長度自變量也是不合法的,使用對象的不定長度自變量不能和方法相同。
package src.week2; public class p{ public static void main(String[] args) { some a=new some(); some.other o=a.new other(); o.doother(); } } class some{ static int x=10; int y=7; class other{ void doother() { System.out.println(x); System.out.println(y);//出現錯誤,static內部類不可存取外部類非static成員。 } } }
內部類自己能夠存取外部類的成員。雖然將外部類看成名稱空間,但也算是個獨立的類,它能夠存取外部類static成員,但不可存取外部類非static成員。
package src.week2; import java.math.BigDecimal; public class k { public static void main(String[] args) { int x=100; int u=100; System.out.println(x==u); BigDecimal a=new BigDecimal(1); BigDecimal b=new BigDecimal(1); System.out.println(a==b); } }
爲何結果是true,false。
Integer x=100; Integer u=new Integer(100);
獲得的結果是false。書上只說了以「」包括的字符串,只要內容徹底相同,不管其出現幾回,JVM都只會創建一個String實例,並在字符串池中進行維護。「==」用於指向對象徹底相同兩個實例纔會出現true。而int類型也是如此,只有 Integer u=new Integer(100);新建一個實例纔會指向不一樣。
a3.exchange(bounce);System.out.printf("%n%d", a3.exchange(bounce));
但發現得出的是奇怪的數字並非正確結果。因而我開始調試代碼,發如今函數末位的返回值是正確的,我又在函數末位加上
System.out.printf("%n%d", this.bounce);
發現結果是正確的。最後發現錯誤所在,上述代碼輸入有誤,不該該是"%n%d", a3.exchange(bounce)而應該是"%n%d", a3.bounce。最後能夠獲得正確答案。
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 120/120 | 1/1 | 16/16 | 開始了JAVA學習的第一步! |
第二週 | 346/466 | 1/2 | 23/36 | 瞭解並學習了Java基礎語法 |
第三週 | 364/830 | 1/3 | 21/57 | 進一步瞭解java設計語句 |
計劃學習時間:18小時
實際學習時間:21小時
改進狀況:能夠不參照書本打出想要的代碼了,雖然有時仍是會編譯出錯,但相較於上一週已經有了很大的進步。