Java Reference
In-Depth Information
switch statement
determines which
operator button
user clicked
else
statement
cases for each
operator
result formatted
and displayed in
lcd TextField
FIGURE 6-33
No matter what the operand, in line 227, the format() method from the
DecimalFormat class automatically converts the value op1 to a String. If you had
not used the format() method, you would have had to convert the value using a
method such as toString() in order for it to be placed in the TextField because
TextFields can accept only String data.
Searching for the Equal Button
Notice that the previous switch structure does not contain a case for the
equal button, although lines 227 and 228 execute for all operands after the first
one. When the user specifically clicks the equal button, the lastOp variable is
assigned a value of 14, which is the index number of the equal button. Because a
value of 14 does not match any of the cases, execution will pass directly to line
230, as shown in Figure 6-34.
230
if ( i==14 ) first = true ; // equal button
231
else lastOp = i; // save last operator
232
} // end else
233
break ;
234
} // end of switch(i)
235
} // end of if
236
} // end of for
237
} // end of actionPerformed
238
FIGURE 6-34
 
Search WWH ::




Custom Search