Outline • • • Common Errors in Selection Statement Logical Operators switch Statements Formatting Console Output For 迴圈 (loop)
Common Errors in Selection Statement • Error 1: if ( radius >= 0 ) area = radius * 3. 14; System. out. println(“The area is” + area);
if ( radius >= 0 ) { area = radius * 3. 14; System. out. println(“The area is” + area); }
• Error 2: if ( radius >= 0 ); { area = radius * 3. 14; System. out. println(“The area is” + area); }
• Error 3: if ( a = true ) { System. out. println(“a is true !!” ); }
if ( a == true ) { System. out. println(“a is true !!” ); }
Logical Operators Operator ! && Name not and || or ^ exclusive or Description logical negation logical conjunction logical disjunction logical exclusive
num = 1 true Statement 2 true Statement else false num = 2 false else false
程式: Switch. Case. java 課本 p. 117、p. 118
Formatting Console Output Specifier %b %c %d %f %e %s Output a Boolean value a character a decimal integer a float-point number a number in standar scientific notation a string Example true or false ‘a’ 200 45. 460000 4. 556000 e+01 “Java is cool”
EX: int count = 5; double amount = 45. 56; String str = “Java”; System. out. printf(“count is %d”, count); System. out. printf(“amount is %f”, amount); System. out. printf(“str is %s”, str); 練習: 將上述寫成程式.