基本類型與類類型的概念
在java裏使用數組和字符串
封裝的概念
在java定義函數
重載的概念
static的概念html
由於程序不少因此我截取部分java
猜數字數組
public class Guess { public static void main(String[] args) { Scanner console = new Scanner(System.in); int number = (int) (Math.random() * 10); int guess; do { System.out.print("猜數字(0 ~ 9):"); guess = console.nextInt(); } while(guess != number); System.out.println("猜中了XD"); } }
使用BigDecimalapp
import java.math.BigDecimal; public class DecumalDemo { public static void main(String[] args) { BigDecimal operand1 = new BigDecimal("1.0"); BigDecimal operand2 = new BigDecimal("0.8"); BigDecimal result = operand1.subtract(operand2); System.out.println(result); BigDecimal op1 = new BigDecimal("0.1"); BigDecimal op2 = new BigDecimal("0.1"); BigDecimal op3 = new BigDecimal("0.1"); BigDecimal result2 = new BigDecimal("0.3"); if(op1.add(op2).add(op3).equals(result2)) { System.out.println("等於 0.3"); } else { System.out.println("不等於 0.3"); } } }
基本類型打包dom
public class IntegerDemo { public static void main(String[] args) { int data1 = 10; int data2 = 20; Integer wrapper1 = new Integer(data1); Integer wrapper2 = new Integer(data2); System.out.println(data1 / 3); System.out.println(wrapper1.doubleValue() / 3); System.out.println(wrapper1.compareTo(wrapper2)); } }
數組複製函數
class Clothes { String color; char size; Clothes(String color, char size) { this.color = color; this.size = size; } } public class Copy { public static void main(String[] args) { Clothes[] c1 = {new Clothes("red", 'L'), new Clothes("blue", 'M')}; 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); //淺層複製的結果 Clothes[] c3 = {new Clothes("red", 'L'), new Clothes("blue", 'M')}; Clothes[] c4 = new Clothes[c3.length]; for(int i = 0; i < c3.length; i++) { Clothes c = new Clothes(c3[i].color, c3[i].size); c4[i] = c; } c3[0].color = "yellow"; System.out.println(c4[0].color); //深層複製的結果 } }
1到100學習
public class OneTo100 { public static void main(String[] args) { StringBuilder oneTo100 = new StringBuilder(); for (int i = 1; i < 100; i++) { oneTo100.append(i).append('+'); } System.out.println(oneTo100.append(100).toString()); } }
儲蓄ui
import java.util.Scanner; class CashCard { private String number; private int balance; private int bonus; CashCard(String number, int balance, int bonus) { this.number = number; this.balance = balance; this.bonus = bonus; } void store(int money) { if(money > 0) { this.balance += money; if(money >= 1000) { this.bonus++; } } else { System.out.println("儲值是負的?你是來搗亂的嗎?"); } } void charge(int money) { if(money > 0) { if(money <= this.balance) { this.balance -= money; } else { System.out.println("錢不夠啦!"); } } else { System.out.println("扣負數?這不是讓我儲值嗎?"); } } int exchange(int bonus) { if(bonus > 0) { this.bonus -= bonus; } return this.bonus; } int getBalance() { return balance; } int getBonus() { return bonus; } String getNumber() { return number; } } public class CardApp { public static void main(String[] args) { Scanner console = new Scanner(System.in); CashCard card1 = new CashCard("A001", 500, 0); card1.store(console.nextInt()); CashCard card2 = new CashCard("A002", 300, 0); card2.store(console.nextInt()); CashCard card3 = new CashCard("A003", 1000, 1); card3.store(console.nextInt()); System.out.printf("明細 (%s, %d, %d)%n", card1.getNumber(), card1.getBalance(), card1.getBonus()); System.out.printf("明細 (%s, %d, %d)%n", card2.getNumber(), card2.getBalance(), card2.getBonus()); System.out.printf("明細 (%s, %d, %d)%n", card3.getNumber(), card3.getBalance(), card3.getBonus()); } }
不太理解爲何要進行基本類型打包,基本類型打包有什麼做用?this
這周我將NetBaens換成了IDEA,
對1到100那個程序的運行結果截圖的時候由於結果太長,很難截圖,感受應該是有結果自動換行功能的,找了一下,發現了應該是自動換行按鈕的東西
還有顯示代碼行數的功能
等等設計
都是在抄書,因此沒問題。
感受本週的學習難度明顯上升了,有不少新概念出現,例如對象、參考等概念感受理解起來就很麻煩,很難把這些概念固定在印象裏。
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 1000行 | 16篇 | 300小時 | |
第一週 | 20/1000 | 1/16 | 8/300 | |
第二週 | 71/1000 | 2/16 | 17/300 | |
第三週 | 320/1000 | 3/16 | 30/300 |