Java help! (need help asap!!!!)

RallyeRedSi

Member
:
2008 Civic Si
I have to create two pattern using nested loops and I'm really lost
the patterns are

*
**
***
****
*****
****
***
**
*

and

*****
****
***
**
*
**
***
****
*****
 
This isn't exactly Java and I did not test this code but try this and so it probably doesn't compile but you can take the ideas from it. Treat it as pseudo code.

// The string that holds the stars
string str = "*";
// Number of times the graphic changes direction
int MAX_RISES = 1;
// The number of stars at the peak
int PEAK = 5

for (int i = 0; i < MAX_RISES; i++)
{
for (int j = 0; j < PEAK; j++)
{
// Modulus operator. If zero its even number, if one its odd
if (i % 2 == 0)
{
str += "*";
}
else
{
str -= "*";
}
// PRINT THE STAR STRING HERE
}
}

And well the second one is the same but you change things around.
 
wait til they make you do that exercise using recursion. it will blow your mind


and can you do string manipulation like that in java? the += and -=? i don't think i've ever done that but that doesn't mean it can't be done. i also can't say i've spent a lot of time in java and in most languages string manipulation is harder than it needs to be
 
also the biggest hint about programming in school is to go to class. in intro courses the teacher will focus on the topics they want you to use to accomplish the homework. usually there are thousands of ways to write any program but they will help narrow down what to use and probably walk you through similar examples.
 
Back