發表文章

目前顯示的是 3月, 2019的文章

資策會|JAVA|FileReader 與 BufferedReader 差異

StackOeverFlow 是個好地方 以下有一個問題... 給你一篇文章,然後利用 FileReader 與 BufferedReader 來分別得到bytes, chars,line 各為多少! 這邊我煩惱了許久,於是上網發問啦! What's different between using FileReader() and BufferedReader() to get the char? [duplicate] 我的疑問是在於 BufferedReader.readline 會自己吃掉換行字元 \n 那為何只得到10個,與21相去甚遠 解決的程式碼如下 : public static void workWithFileReader() { int i; long len; // int countChar = 0; //每讀到一個byte +1 int countLine = 0; //每讀到一個換行 char(10) +1 int lineTerminators = 0; File file = new File("C:\\Users\\USER\\Downloads\\Sample.txt"); len=file.length(); try { FileReader fr = new FileReader(file); while((i=fr.read()) != -1) { System.out.print((char)i); countChar++; if((char) i == 10) { countLine++; lineTerminators++; } if((char) i == 13) { countLine++; lineTerminators++; } } System.out.println("----------------------"); System.out.println("共有"+len+"個位元組"); System.out.p...

資策會|JAVA|封裝

什麼是封裝? 目的是在於提升資料存取安全性與隱藏資料 為甚麼要這樣做? 避免出包!! ................................ 封裝基本概念 JAVA資料封裝的基本就是類別(class),然後使用private , default , protected , public四種存取修飾子作為封裝權限的等級。 直接看扣吧! 阿文開了一家文具店,只賣筆!然後自己設計了一個標價系統!! public class PenNG { public String brand = "A Min Brand"; public double price = 0.0; public void show() { System.out.println("Brand is : " + brand); System.out.println("Price is : " + price); } } 程式完成囉!準備開業!為了慶祝開幕,一支筆10塊!! public class PenTest { public static void main(String[] args) { PenNG p = new PenNG(); p.price = -10; p.show(); } } 人生就是這個but! 標錯價啦! 結過造成生意火爆,大賣5000支,賠了五萬元,打響名號!! 可喜可賀! Brand is : A Min Brand Price is : -10.0 於是阿明決定google爬文,修正錯誤,不讓錯誤一再發生。 public class PenGood { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(dou...

資策會|JAVA|類別、方法、存取修飾字

圖片
修飾子適用的場合 參考 tomlinNTUB

Blogger|格式|Google Code Prettify part 2

延續 上一篇 後來找到這篇,一看就懂啦! 【Google Code Prettify】取代 Syntaxhighlighter 程式碼區塊 。於是照文章內容再試試看。 範例 剛好這個是作業,題目如下: 請設計一隻程式由鍵盤輸入三個整數,分別代表西元yyyy年,mm月, dd日,它會顯示是該年的第幾天 例:輸入 1984 9 8 三個號碼後,程式會顯示「西元1984年,第250天」 (提示1:Scanner,陣列) (提示2:需將閏年條件加入) (提示3:輸入月份如為2月,則日期不該超過29天) import java.util.Scanner; /* * 2019/03/11 * while(true)無限迴圈 搭配 符合條件 break * 能用方法解決就用方法 * * */ public class AdvancedQuestion1 { int[] yyyymmdd =new int[3]; public AdvancedQuestion1() { // inputYYYYMMDD(); // howManyDay(); } public void inputYYYYMMDD() { while(true) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入西元yyyy年mm月dd日 : "); if(scanner.hasNextInt()) { yyyymmdd[0]=scanner.nextInt(); //年 yyyymmdd[1]=scanner.nextInt(); //月 yyyymmdd[2]=scanner.nextInt(); //日 scanner.close();//輸入完就先關啊 不然要泛舟喔XD if(checkMonthOfDays()) { break; } System.out.println("Please input the correct month or days"); } } } pub...

資策會|JAVA|2019/3/10回顧 ft.實體變數、區域變數、static變數

回顧       到目前為止進度算是跟得上,但之前開課前先唸的扣打好像快用完了。囧。所幸的是,位置終於固定了,可以多睡個20分鐘啦! 預習       說到這,真的要提起預習的好處啦!我覺得是這樣啦!不管自己預習有沒有看懂,至少在腦海裡會有個輪廓在那邊。在上課時,腦袋也比較容易接受從而去思考老師說的觀念及新東西。而不會先愣在那說,這啥!?哪尼口類!? 二刷       有預習,上課自然就變成二刷,像是大家看了同樣的電影多次後,總是會注意到上一次沒看到的細節,因為對於主要想表達的變成能快速接受吸收,而有多的注意力去發現新的細節。以實體變數、區域變數、static變數來說,老師在說明時,很容易可以把他們各自的不同、關係、位階給連起來。我想如果是我第一次遇到,想必是不太容易做到的。 實體變數、區域變數、static變數      既然都提到了,就來試著說明看看三者的關係。像是地球、自己、自己手上的麵包為例。 static變數  <---> 地球 實體變數    <---> 自己 區域變數    <---> 自己手上的麵包 他們的關係就像這樣子! 人存在的時候 , 地球老早就存在啦! 但是!地球存在的時候,你可能還沒出生呢! 自己手上的麵包,當我執行完吃麵包這個方法後,麵包就消失了! 以上,報告完畢! JAVA上課進度     有種老師不論何種章節,觀念難不難,都是均速再進行課程的樣子。好處應該是讓我們覺得JAVA其實沒有這麼難吧。哈! 以下是上次到這次的進度,這邊只打個大概,忽然有什麼人生體悟再來進行補充XD 迴圈 continue , break 與標籤的運用(少見) 無窮迴圈 while(true){...} 目前蠻喜歡這個的 搭配 break 頗好用! 方法的宣告與使用 傳遞回傳值,還沒有詳細介紹 覆載overloading 不只方法,在類別裡的建構子也可 物件導向概論 物件 及 類別 物件參考變數 這邊有...

資策會|JAVA|邏輯運算子之短路運算

短路運算 這是上課時有提到的,今天就來來驗證看看。 邏輯運算子 短路運算 : ||  (or) , &&(and) 非短路運算 :  |  (or) , &(and) 測試 拿or來測試囉!                 int i = 10; int j = 20; if (( i +=20)==30 || ( j +=20) == 40 ) { System. out .println( i ); System. out .println( j ); } int x = 10; int y = 20; if (( x +=20)==30 | ( y +=20) == 40 ) { System. out .println( x ); System. out .println( y ); } 印出後的結果是!!!! 30 20 30 40 拿and來測試囉!                 int i = 10; int j = 20; if (!(( i +=20)==40000 && ( j +=20) == 40 )) { System. out .println( i ); System. out .println( j ); } int x = 10; int y = 20; if (!(( x +=20)==40000 & ( y +=20) == 40 )) { System. out .println( x ); System. out .println( y ); } 印出後的結果是!!!! 30 20 30 40 小結 這驗證了短路運算下只要...

Blogger|格式|Google Code Prettify

測試 Google Code Prettify 嗨嗨 這篇主要是拿來測試如何在Blogger上使用Google Code Prettify的。 主要是參考這篇文章 [Blogger] 如何在 Blogger 顯示程式碼 - Google Code Prettify 。 如內文所說的放置在下列兩者語法之間,即可完成囉! <pre class = "prettyprint" > your code... </pre> <code class = "prettyprint" > your code... </code> 測試看看吧! System. out .println( "Hello World!!" ); 在HTML長這樣! <code class="prettyprint"> <span class="s1">System.</span><span class="s2"><b><i>out</i></b></span><span class="s1">.println(</span>"Hello World!!"<span class="s1">);</span> </code> 再測試看看! public boolean isLeapYear() { if (( yyyymmdd [0] % 4 == 0 && yyyymmdd [0] % 100 != 0) || ( yyyymmdd [0] % 400 == 0)) { return true ; } return false ; } 要如何變成黑色變景啊!? Google Code Prettify 支援不同的 skin  ,參考這篇再做研究看看好了。 ...