JAVA 2010 15 Outline Common Errors in Selection

  • Slides: 19
Download presentation
JAVA 2010. 15

JAVA 2010. 15

Outline • • • Common Errors in Selection Statement Logical Operators switch Statements Formatting

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 )

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.

if ( radius >= 0 ) { area = radius * 3. 14; System. out. println(“The area is” + area); }

 • Error 2: if ( radius >= 0 ); { area = radius

• 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

• Error 3: if ( a = true ) { System. out. println(“a is true !!” ); }

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 Operators Operator ! && Name not and || or ^ exclusive or Description logical negation logical conjunction logical disjunction logical exclusive

switch Statements 範例: switch (num) { case 1: case 2: default: } statement 1;

switch Statements 範例: switch (num) { case 1: case 2: default: } statement 1; break; statement 2; break; statement else; System. exit(0);

num = 1 true Statement 2 true Statement else false num = 2 false

num = 1 true Statement 2 true Statement else false num = 2 false else false

程式: Switch. Case. java 課本 p. 117、p. 118

程式: Switch. Case. java 課本 p. 117、p. 118

Formatting Console Output Specifier %b %c %d %f %e %s Output a Boolean value

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”;

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); 練習: 將上述寫成程式.