d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso Beginner Java Help (nested For Loops) > Homework Problem
Add Reply New Topic New Poll
Member
Posts: 4,635
Joined: May 3 2009
Gold: 1,232.52
Sep 30 2014 12:53pm
Two examples of what the output should look like.

User enters 5
Code
0 * *
1 * *
2 *
3 * *
4 * *


User enters 4
Code
0 * *
1 **
2 **
3 * *


What I've written so far (just an outline) (row is obtained from a scanner)
Code
for(int i = 0; i < row; i++)
{
System.out.print(/*?*/);
for(k = 1; k < /*?*/; k++)
{
if(/*?*/)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println(""); //Returns after each row printed


I really have no idea how to do this. My code is just an outline so far. Any help is appreciated.

This post was edited by KitsuneYosh on Sep 30 2014 01:06pm
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 30 2014 06:29pm
So the first print will just be the row number.

the variable "k" will have the same range as i since it's a square, so k = 0 and < row (also make sure to initialize the variable).

For the "if", the pattern is that the the first diagonal of asterisk (from left to right) will be the same as the current row number so when k = i, the second is when (row-1)-k = i (since it's 0-4 and not 1-5)
Since it has to follow only one of those conditions, you have to put an "or" in between them.


If you have any other questions, let me know.

This post was edited by ShadowFiend on Sep 30 2014 06:30pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 30 2014 07:54pm
Quote
I really have no idea how to do this. My code is just an outline so far. Any help is appreciated.


if that's what your teacher started you off with, ignore it.

if you're having problems understanding loops, then the key is to work on just one loop at a time. start with the inside loop. if it makes it easier for you to understand, you can just copy/paste a lot of code to get it working, then slowly replace the code with loops.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll