Web site translator - Chapter 6: Controlling Program Flow with Loops 139
Chapter 6: Controlling Program Flow with Loops 139 You can create variables to refer to those values. After the forloop in Listing 6-5, I declare a variable named killer. Just as int numGuessesdeclares a numGuessesvariable is of type int, and just as String myTitledeclares variable myTitleto be of type String, so Suspect killerdeclares variable killerto be of type Suspect. I declare int numGuessesin Listing 6-1, and I declare String myTitlein Listing 4-6. Being of type Suspectmeans that you can have values mustard, plum, green, and so on. So in Listing 6-5, I give the killervariable the value peacock. (At this point in the code, if I don t type the longer dotted name Suspect.peacock, the Java compiler gets confused.) In a similar way, I create variables sceneOfTheCrimeand murderWeapon toward the end of Listing 6-5. Both these variables have enumtypes, and both are given appropriate values (values like Room.studyand Weapon. leadPipe). In Listing 6-5, all the enumtype declarations are outside the mainmethod. (For example, the line that begins with enum Suspectis before the start of the mainmethod.) Java doesn t allow you to put an enumtype declaration inside a method. That s because an enumtype declaration is really a Java class in disguise. For more insight on enumtypes, see Chapter 9. Anatomy of an enhanced for loop The enhanced and un-enhanced forloops have a lot in common. Figure 6-8 illustrates the point. Listing 6-2: for (int count = 1; count <= 10; count++) The type of The range of values The name of value that a that variable takes the variable variable can on during execution have of the loop Figure 6-8: Comparing Listing 6-5: for (Suspect mySuspect : Suspect.values()) for loops.
Our Colorado hosting facilities are located in Little Rock, Colorado. Colorado web hosting datacenter which we have is linked on five major US backbones which gives you assurance that your site will be online 24/7 a day. More details you can find out in Web Hosting Colorado part.