PDA

View Full Version : how would i loop this


raptrex
03-07-2009, 07:23 AM
ok so i have 4 ints: sales1,sales2,sales3,sales4
they will each have a whole number ie. 100,10920,2901 ect
i need to display a star for every hundred from sales1,2,3,4 after its rounded ie 100 = *, 200 = **

all i can think of is an if else statement like this
if (50 <= sales1 <=150)
{
System.out.println("*");
}
else if (150 <= sales1 <= 250)
{
System.out.println("**");
}
else if (250 <= sales1 <= 350)
{
System.out.println("***");
}
else if (350 <= sales1 <= 450)

this way would take to long as the int can be like in the thousands
but im sure theres an easier/better way to do this so any ideas?

so basically, im trying to get this to be like this:
Store 1: ***************
Store 2: ************
Store 3: ********************
Store 4: *****************

abduraooft
03-07-2009, 08:10 AM
if (50 <= sales1 <=150)should be

if(sales1 >= 50 && sales1<=150)
and similarly for others.

riwan
03-07-2009, 10:21 AM
sales1stars = sales1 % 100;
for (i = 1; i<= sales1stars; i++)
{
System.out.println("*");
}

RedMatrix
03-07-2009, 12:47 PM
riwan, dividing by modulus only leaves the remainder. (which will be less than 100)

So, if $sales1 = 547, then $sales1 % 100 would yield 47, not 5.

I think this may work:

sales1 = 547
cents1 = sales1 / 100;
remainder1 = sales1 % 100;

while (cents1 >= 1) {
echo '*';
cents1--;
}
if (remainder1 >= 50) { echo '*';}



Repeat with other sales variables.

raptrex
03-07-2009, 06:55 PM
thanks redmatrix, it worked with a bit of modifying :P

riwan
03-08-2009, 12:45 PM
lol, well just trying to help, I'm half awake at the time, But you get what my point is

riwan, dividing by modulus only leaves the remainder. (which will be less than 100)
Repeat with other sales variables.

RedMatrix
03-09-2009, 02:39 AM
raptrex, Wow, I'm glad you were able to find a tweak that worked!

riwan, it's cool.