94 Part II: Writing Your Own Java Programs (Web file server)
94 Part II: Writing Your Own Java Programs Listing 4-7: (continued) out.println(quarters + quarters ); out.println(dimes + dimes ); out.println(nickels + nickels ); out.println(cents + cents ); } } A run of the code in Listing 4-7 is shown in Figure 4-9. You start with a total of 248 cents. Then quarters = total / 25 divides 248 by 25, giving 9. That means you can make 9 quarters from 248 cents. Next, whatsLeft = total % 25 divides 248 by 25 again, and puts only the remainder, 23, into whatsLeft. Now you re ready for the next step, which is to take as many dimes as you can out of 23 cents. Figure 4-9: Change for $2.48. Initialize once, assign often Listing 4-7 has three lines that put values into the variable whatsLeft: int whatsLeft = total % 25; whatsLeft = whatsLeft % 10; whatsLeft = whatsLeft % 5; Only one of these lines is a declaration. The other two lines are assignment statements. That s good because you can t declare the same variable more than once (not without creating something called a block). If you goof and write int whatsLeft = total % 25; int whatsLeft = whatsLeft % 10;
Hosting services offered by our company comes with free domain name if you pay at yearly basis. Find out more at New Orleans Web Hosting services.