# Need Help! For loops and if/else

• February 11th, 2012, 04:16 PM
kram
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();
}
• February 11th, 2012, 04:29 PM
newbie
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)
• February 11th, 2012, 05:22 PM
kram
Re: Need Help! For loops and if/else
Thank You sir, why do i make things so complicated.