Chapter 4: Making the Most of Variables and (Web hosting ratings)
Chapter 4: Making the Most of Variables and Their Values 93 Of course, the old minus sign is available too (but not for Stringvalues). apples = fruit - oranges; Use an asterisk (*) for multiplication and a forward slash (/) for division. double rate, pay; int hours; rate = 6.25; hours = 35; pay = rate * hours; System.out.println(pay); For an example using division, refer to Listing 4-3. When you divide an intvalue by another intvalue, you get an intvalue. The computer doesn t round. Instead, the computer chops off any remainder. If you put System.out.println(11 / 4)in your program, the computer prints 2, not 2.75. To get past this, make either (or both) of the numbers you re dividing doublevalues. If you put System.out.println(11.0 / 4) in your program, the computer prints 2.75. Another useful arithmetic operator is called the remainder operator. The symbol for the remainder operator is the percent sign (%). When you put System.out. println(11 % 4)in your program, the computer prints 3. It does this because 4 goes into 11 who-cares-how-many times with a remainder of 3. The remainder operator turns out to be fairly useful. Listing 4-7 has an example. Listing 4-7: Making Change import static java.lang.System.out; class MakeChange { public static void main(String args[]) { int total = 248; int quarters = total / 25; int whatsLeft = total % 25; int dimes = whatsLeft / 10; whatsLeft = whatsLeft % 10; int nickels = whatsLeft / 5; whatsLeft = whatsLeft % 5; int cents = whatsLeft; out.println( From + total + cents you get ); (continued)
Would you like to be a member of headache free web site owner’s community. Now you can. We focus in IX Web Hosting, go go go!