(^196) | Selection and Encapsulation
start with the innermost pair and draw a line connecting them. Do the same
for the others, working your way out to the outermost pair. For example:
3.Here is a quick way to tell whether you have an equal number of opening and
closing parentheses. The scheme uses a single number (the “magic number”),
whose initial value is 0. Scan the expression from left to right. At each
opening parenthesis, add 1 to the magic number; at each closing parenthesis,
subtract 1. At the final closing parenthesis, the magic number should be 0. For
example,
if (((total/scores) > 50 ) && ((total/(scores – 1 )) < 100 ))
0 123 2 1 23 4 32 10
4.Don’t use =<to mean “less than or equal to”; only the symbol <=works.
Likewise,=>is invalid for “greater than or equal to”; you must use >=for this
operation.
5.Don’t compare strings with the ==operator. Use the associated instance
methods such as equalsand compareTo. When testing for alphabetical order, re-
member to convert the strings to the same case before making the
comparison.
6.When comparing values of different types, use explicit casting to clarify how
the values should be converted before comparison.
7.Don’t compare floating-point types for exact equality. Check that the
difference between the values is less than some small amount.
8.In an ifstatement, remember to use a { }pair if the first clause or the else
clause is a sequence of statements. Also, don’t put a semicolon after the right
brace.
9.Test for bad data. If a data value must be positive, use an ifstatement to test
the value. If the value is negative or 0, an error message should be displayed;
otherwise, processing should continue.
10.Take some sample values and try them by hand. Develop a test plan before
you start testing your code.
11.If your application produces an answer that does not agree with a value
you’ve calculated by hand, try these suggestions:
Redo your arithmetic.
Recheck your input data.
Carefully go over the section of code that performs the calculation. If you’re
in doubt about the order in which the operations are performed, insert clar-
ifying parentheses.
if (((total/scores) > 50) && ((total/(scores - 1)) < 100))
やまだぃちぅ
(やまだぃちぅ)
#1