20162311 2016-2017-2 《程序設計與數據結構》第九周學習總結
教材學習內容總結
- 使用XAMPP建立與數據庫連接
- 創建並修改數據庫表
- Linux下安裝MySQL數據庫
- 一些簡單的SQL語句
教材學習中的問題和解決過程
- 問題1:學習實驗樓的MySQL基礎教程時,按照教程上的步驟在自己的虛擬機上安裝MySQL,裝好之后啟動MySQL,輸入命令
mysql -u root
卻出現錯誤了
- 解決方案:關於這個問題,我問了王老師。王老師說他也遇到過這個問題,讓我百度搜索MySQL 教程
在命令mysql -u root
之后加一個參數-p
,然后輸入密碼就可以了。
后來我又查了一下-u
和-p
的意思
代碼調試中的問題和解決過程
-
問題1:在做老師布置的mini dc作業時,把MyDC類寫好了,編譯卻出錯了
token是String類型,而evalSingleOp方法的第一個參數是Char類型 -
解決方案:我詢問了馬平川同學,他告訴我一個方法
toCharArray()
,這個方法可以把字符串轉換為一個新的字符數組。我查了一下API文檔
於是我把第一個參數改成token.toCharArray()[0]
,即轉成字符數組后,取第一個元素,因為token變量只有一個字符。改完之后就能正常編譯運行了。 -
問題2:按照老師要求,用分支語句寫一個簡單的計算器,源代碼如下
public class Calc {
public static void main(String [] args) {
int result = 0;
if (args.length != 3) {
System.out.println("Usage: java Calc operato1 operand(+ - * / %) operator2");
}
else if (args[1].equals("+")){
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
}
else if (args[1].equals("-")){
result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
}
else if (args[1].equals("*")){
result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
}
else if (args[1].equals("/")){
result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
}
else if (args[1].equals("%")){
result = Integer.parseInt(args[0]) % Integer.parseInt(args[2]);
}
System.out.println(args[0] + args[1] + args[2] + " = " + result);
}
}
能通過編譯,計算加、減、除和取余都正常,但計算乘的時候結果總是0
- 解決方案:我問了老師,老師告訴我
args[1].equals("*")
這里比較時,*
會被看成通配符,這樣就不會執行后面的語句,result的值也一直是0,可以用字母“x”代替。我改了之后就沒問題了。不過分支語句不止if...else
,還有switch...case
,如果我用switch...case
來寫,是不是就可以避免這個問題呢?於是我試了一下
public class NewCalc{
public static void main (String [] args){
int result = 0;
final char ADD = '+';
final char SUBTRACT = '-';
final char MULTIPLY = '*';
final char DIVIDE = '/';
final char REMAINDER = '%';
if (args.length != 3){
System.out.println("Usage: java Calc operator1 operand(+ - * / %) operator2");
}
else switch (args[1].toCharArray()[0]){
case ADD:
result = Integer.parseInt(args[0]) + Integer.parseInt(args[2]);
break;
case SUBTRACT:
result = Integer.parseInt(args[0]) - Integer.parseInt(args[2]);
break;
case MULTIPLY:
result = Integer.parseInt(args[0]) * Integer.parseInt(args[2]);
break;
case DIVIDE:
result = Integer.parseInt(args[0]) / Integer.parseInt(args[2]);
break;
case REMAINDER:
result = Integer.parseInt(args[0]) % Integer.parseInt(args[2]);
break;
}
System.out.println(args[0] + args[1] + args[2] + " = " + result);
}
}
可是仍然不行,我又問了同學才知道,原來在命令行里輸入*
都會被當成通配符,無法與稱號比較,只能用字母“x”代替了
代碼托管
(statistics.sh腳本的運行結果截圖)
上周考試錯題總結
- 錯題1:
test.txt 中的內容是:
No Name Mark Percent
01 tom 69 91
02 jack 71 87
03 alex 68 98
把第四列提取出來的Linux命令是:
A .cut -f 1 test.txt
B .cut -f 2 test.txt
C .cut -f 3 test.txt
D .cut -f 4 test.txt
正確答案: D
分析:cut 命令中第n列不是從0開始計數的。
- 錯題2:
sort.txt中的內容是:
aaa:10:1.1
ccc:20:3.3
ddd:40:4.4
bbb:30:2.2
eee:50:5.5
用“sort -t: -nk2 sort.txt”排序后的第二行是:
A .aaa:10:1.1
B .ccc:20:3.3
C .ddd:40:4.4
D .bbb:30:2.2
E .eee:50:5.5
正確答案: B
分析:對每二列按數字升序排序
- 錯題3:
If an exception is not caught, a program will __________________________ (如果不捕獲異常,程序將會____ ).
A .not compile(不編譯)
B .terminate abnormally(異常終止)
C .print a message and continue executing(輸出消息並繼續執行)
D .all of the above(以上情況都會發生)
E .neither a, b nor c(abc都不對)
正確答案: B
分析:如果一個異常沒有被catch塊處理,那么程序會異常終止
- 錯題4:
The getMessage method of the Exception class prints out the stack trace, which helps the user to track down the source of the exception(Exception類的getMessage方法輸出棧跟蹤信息,有助於找到產生異常的源).
A .true
B .false
正確答案: B
分析:printStackTrace
方法打印棧跟蹤
- 錯題5:
Which of the following file streams should be explicitly closed to ensure that written data is properly retained(下面哪個文件輸入流應該顯式關閉,以確保數據能正確保存下來)?
A .output
B .input
C .error
D .writable
E .readable
正確答案: A
分析:輸出文件流應該使用關閉方法顯式關閉,以便所有數據被正確保留
結對及互評
點評過的同學博客和代碼
思考
本周學習的數據庫感覺和之前學的內容不大一樣,不僅僅是敲一些代碼。由於是第一次接觸數據庫,所以有很多地方都不懂,哪怕老師給了一些教程,也無法完全理解。數據庫的學習應該不是一個短暫的過程。老師說這周之后就要開始嘗試做一些項目,我想一定會有用到數據庫的地方,到時候邊做邊學,加深理解,應該效果要比只看老師的教程耀好一些。
學習進度條
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 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 | |
第九周 | 367/4698 | 1/11 | 15/155 |
-
計划學習時間:15小時
-
實際學習時間:15小時
-
改進情況:數據庫不是一周就能解決的,這周花的時間有些少。下周開始不用寫博客了,應當把多出的這部分時間用來深入學習數據庫,感覺這部分知識挺重要的。