try-catch
語句
unchecked
(免檢)異常,其他屬於checked
(必檢)異常checked
異常不進行處理或拋出的話,沒法經過編譯throw
和throws
的做用
throw
用來拋出一個異常對象throws
用來聲明異常(必檢異常),throws Exception
緊跟在方法以後InputStream
,OutputStream
是全部字節流的父類解決方案:我到雲班課上提問,老師提示我查查10在ASCII碼中的含義,因而我查了ASCII碼錶,發現10是換行符。因而我在虛擬機中用od命令查看from.txt文件的內容,發現最後果真有個換行符。
但是我並無敲回車,也沒有換行,爲何會有換行符呢?老師在講解說,在Linux下,文件的每行會自動加一個換行符,咱們是看不到的,要用od命令查看才能知道,並且若是實在Windows下,是自動加回車(「/r」)。html
問題2:書上的程序10.2有一行代碼,用到了這樣一個方法:Integer.praseInt();
,不明白這個方法的做用。java
解決方案:查找API幫助文檔。praseInt()
是Integer
類中的一個靜態方法,返回值類型爲Int,做用是將字符串參數做爲有符號的十進制整數進行解析。
書上的代碼是要將一串String
類型的數字轉化成Int
類型git
import java.util.Scanner; public class AverageNumberException { public static void main (String[] args) { String num; int num1, sum = 0, average, i = 0; final int TIMES = 10; Scanner scan = new Scanner(System.in); System.out.println("Enter 10 integers: "); System.out.print(" "); num = scan.nextLine(); while (i>=0 && i<10) { try { num1 = Integer.parseInt(num); sum += num1; i++; } catch(NumberFormatException e) { System.out.println("The integer you entered is invalid! Please input again."); } System.out.print(" "); num = scan.nextLine(); } average = sum / TIMES; System.out.println("Average: " + average); } }
運行結果
我輸了出去異常的數字以外,我輸了11個數程序才結束數據結構
我把循環條件改了一下:ide
import java.util.Scanner; public class AverageNumberException { public static void main (String[] args) { String num; int num1, sum = 0, average, i = 0; final int TIMES = 10; Scanner scan = new Scanner(System.in); System.out.println("Enter 10 integers: "); while (ture) { try { System.out.print(" "); num = scan.nextLine(); num1 = Integer.parseInt(num); sum += num1; i++; } catch(NumberFormatException e) { System.out.println("The integer you entered is invalid! Please input again."); } if (i==9) { break; } } average = sum / TIMES; System.out.println("Average: " + average); } }
但結果倒是隻能輸9個數,我想確定是循環出了問題,因而我把if(i==9)
改爲了if(i==10)
,改好以後就能輸入10個數了(除了那個異常的數)。我想緣由是輸入第一個整數後,i自加1,從0變成1,這樣循環下去,到第九個有效整數時,i就變成了9,也就退出循環了。我又看了一下最初的代碼,發現是一樣的問題,因而也改了一下工具
import java.util.Scanner; public class AverageNumberException { public static void main (String[] args) { String num; int num1, sum = 0, average, i = 0; final int TIMES = 10; Scanner scan = new Scanner(System.in); System.out.println("Enter 10 integers: "); System.out.print(" "); num = scan.nextLine(); while (i>=0 && i<9) { try { System.out.print(" "); num = scan.nextLine(); num1 = Integer.parseInt(num); sum += num1; i++; } catch(NumberFormatException e) { System.out.println("The integer you entered is invalid! Please input again."); } } average = sum / TIMES; System.out.println("Average: " + average); } }
可結果倒是,能夠輸10個整數了,但算出的結果卻不對。我又仔細看了一下代碼,發現雖然循環對了,可是,我在循環外輸入了第一個整數,這個整數沒有被轉化成Int類型的值並加到sum中去,因此至關於少加了一個數。因而我在循環外加上了num1 = Integer.parseInt(num);
和sum += num1;
,這樣就沒有問題了。學習
(statistics.sh腳本的運行結果截圖)測試
Which of the following methods are included with any object that implements the Iterator interface? (下面哪一個方法包含了實現Iterator接口的對象?) A .next B .hasNext C .toString D .all of the above(以上都正確) E .a and b(a和b) 正確答案: D
分析:next
和hasnext
是Iterator
中的抽象方法,toString
是Object
中的方法,而全部類都是Object
的子類,因此Iterator
中也有toString
方法。this
Suppose Animal is an interface that specifies a single method – speak. Now suppose the Dog class implements the Animal interface. In addition to the speak method, the Dog class also has a method called wagTail. Now consider the following code(假設Animal是一個指定了單一方法的接口--speak。如今假設Dog類實現了Animal接口。除了speak方法外,Dog類還有一個方法wagTail。如今思考下面的代碼:). Animal a = new Dog(); a.wagTail(); Which of the following is true about this code?(關於這段代碼,下面哪項是正確的) A .It will result in a compile-time error(這段代碼會引發編譯時錯誤). B .It will result in a run-time error.(這段代碼會引發運行時錯誤) C .It will call the speak method defined in the Animal interface. (這段代碼將會調用Animal接口中的speak方法) D .It will call the wagTail method defined in the Dog class(這段代碼將會調用Dog類中的wagTail方法). E .none of the above are true. (以上都正確) 正確答案: A
分析:這段代碼會出現編譯時錯誤,Animal
接口中沒有定義wagTail
方法。能夠將其顯式轉換爲Dog
類來調用wagTail
方法。.net
An interface cannot declare any instance variables(接口不能聲明任何實例變量). A .true B .false 正確答案: A
分析:接口不能聲明任何實例對象,但可用來聲明對象引用變量。
FHS(英文:Filesystem Hierarchy Standard 中文:文件系統層次結構標準)定義了兩層規範,第一層是()? A ./etc 應該放置設置文件 B ./ 下面的各個目錄應該要放什麼文件數據 C .針對 /usr 及 /var 這兩個目錄的子目錄來定義 D ./bin 與 /sbin 則應該放置可執行文件 E ./var/log 放置系統登陸文件 F ./usr/share 放置共享數據 正確答案: B
分析:這是Linux的目錄結構,參考Linux 基礎入門(新版)中的第四節。
Java是一門工具,從雲班課裏的視頻能夠看出,理論的東西較少。視頻的大部分時間都是在用敲代碼,也就是實踐。因此向要學好Java,必須得多用,在運用的過程當中找問題,從而學習到更多的東西。所以,不能僅僅照着書上的代碼敲,還要多作課後的程序設計題目,培養本身的面向對象程序設計的思想,這樣才能不斷進步。
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 113/113 | 1/1 | 10/10 | |
第二週 | 294/407 | 1/2 | 15/25 | |
第三週 | 433/840 | 1/3 | 15/40 | |
第四周 | 1169/2009 | 2/5 | 30/70 | |
第五週 | 825/2834 | 1/6 | 15/85 | |
第六週 | 331/3165 | 1/7 | 13/98 | |
第七週 | 738/3903 | 2/9 | 22/120 | |
第八週 | 428/4331 | 1/10 | 20/140 |
計劃學習時間:15小時
實際學習時間:20小時
改進狀況:在PP上花的時間佔大部分,主要仍是作的少,本身寫代碼時經常要翻翻筆記,或者從新看一遍視頻,畢竟只看一次記不住。這樣在作PP時免不了花費大量時間。之後還應當在看視頻時就跟着老師一塊兒作測試,這樣能較快地掌握相關知識。同時書上的內容也要弄懂,否則作PP時還要不停地翻書,查找相關資料。