Some notes in Stanford CS106A(1)

Karel world

1.During make a divider operation

--int x=5; double y = x/2  =>  y=2

we need sth as a double for the purpose of this operation treat it as though it were a double .

--int x=5; double y = (double)x/2  =>  y=2.5

OR

--int x=5; double y = x/2.0  =>  y=2.5

2.Define a const that can't be changed

private static final doule PI=3.14;

final:the value won't change after I give its initial value,

static:there's only one of these for the entire class,

private: it only lives inisde the class.

3.A buggy in boolean 

boolean p = (x !=1) || (x!=2) 

=>correct is : boolean p = (x !=1) && (x!=2) 

4.Scope: lifetime of variable 

5.While cast sth that lose information , like from double to a integer , you have to put in the CAST.

ex. double y = 3.5; int x = (int) y;

(if doesn't lose information like int to double , you dont have to cast.)

6.For versus while

For loop used for definite iteration(Generally we know how many times we want to iterate)

While loop used for indefinate iteration(Generally don't know how many times to iterate beforehand.)

猜你喜欢

转载自www.cnblogs.com/wleaves/p/10491367.html