Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Need Help! For loops and if/else

1. Need Help! For loops and if/else

I don't see what is wrong i wrote it down on paper but here is the task i am trying to accomplish.

Print out a count-by-count matrix of asterisks and percent signs, alternating by row
Example:
if count = 5
output will be:

* * * * *
% % % % %
* * * * *
% % % % %
* * * * *
My Code:

int t = 1;
int k = 2;
for(int i=1; i<=count; i++){ // how many sets of for j(5) COUNT IS USER INPUT but it is not in this code
if(i == t){
for(int j=1; j<=count; j++){ //how many asterisks get printed to a line(5)
System.out.print(ast);
t+=2; // prints on odd rows
}
}
else if(i == k){ // checks when j is = to count(t)
for(int l=1; l <= count; l++){
System.out.print('%');
k*=2; // prints only on even rows
}
}
System.out.println();
}

2. Re: Need Help! For loops and if/else

All you need is 1 for loop and a conditional statement.

for (index to count)
if index is an odd number, print stars
else print percentage
end

To determine if a number is even or odd, use the remainder operator (resources found here)

3. The Following User Says Thank You to newbie For This Useful Post:

kram (February 11th, 2012)

4. Re: Need Help! For loops and if/else

Thank You sir, why do i make things so complicated.