Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

86 HOUR 7:Using Conditional Tests to Make Decisions



  1. In the Categories pane, click Sources if it is not already selected. The
    dialog displays source code properties (see Figure 7.2).

  2. In the Source/Binary Format drop-down, choose JDK 7 and click OK.


FIGURE 7.2
Setting up the NetBeans editor to
support Java 7.


This sets up the source code editorfor all programs in the project to work
with Java 7.
When the Commodity program is run, it produces the following output:
Balance: 530
Quantity: 47

The Conditional Operator
The most complicated conditional statement in Java is the ternary operator ?.
You can use the ternary operator when you want to assign a value or dis-
play a value based on a condition. For example, consider a video game
that sets the numberOfEnemiesvariable based on whether the skillLevel
variable is greater than 5. One way you can do this is an if-elsestate-
ment:
if (skillLevel > 5) {
numberOfEnemies = 10;
Free download pdf