I need a triangle like this
* *** ***** *******
I just know how to print like this :
*
***
*****
*******
*********
Can you guys give me some hints. Thank you very much.
My code is:
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
Members have full access to the forums. Advertisements are removed for registered users.
I need a triangle like this
* *** ***** *******
I just know how to print like this :
*
***
*****
*******
*********
Can you guys give me some hints. Thank you very much.
My code is:
Last edited by Genky; August 19th, 2012 at 08:52 AM.
Does this mean you have to input an odd number?
You would have to print spaces on every line except the last line. How many spaces on the first line? How many on the second? You can get this value with some math and an index variable from one of the loops.
Plan out what you have to make happen and then write the code to do it.
Please don't spoonfeed code. Read this:
The Problem with Spoon-feeding
Last edited by Norm; August 19th, 2012 at 08:13 AM. Reason: spelling
mysTTerE (August 19th, 2012)
I mean I was required to show a isoceles triangle. But I don't know how to edit it in above post.
What is the pattern of what is printed on each line?
Take a piece of paper and draw on it the spaces and *s that you want to print.
What prints on the first line? X spaces and one *
What prints on the 2nd line? Y spaces, one *, Z space(s), one *
Continue writing what goes on the next 3-4 lines.
Now look at the number of leading spaces (X and Y above) and the number of inside spaces (Z) for the lines
and see how they are related to the location of the line.
I saw there are 4 lines. Each line the number of spaces decrease by one, and the number of * increase 1,3,5,7.
Can you compute the number of *s from the number of the line? Or another way would be to use a counter for the number of *s. Start at 1 and increase by 2 for each line.
Look at using loops, one to print the spaces and one to print the *
Last edited by Genky; August 19th, 2012 at 08:54 AM.
A space is just another character. Put it inside of some "s:how to print spaces
" " for a space
vs
"*" for an *
You mean like thisBut it just print:for (int i = 0; i <4; i++) { System.out.print(" "); for (int j = 0; j < 4; j++) { System.out.print("*"); } System.out.println(); }
****
****
****
****
Look at the steps in the algorithm you worked out for the code and get that right first.
Then write the code. Your code obviously doesn't follow the right algorithm.
Can you post the algorithm's steps so we can see how it is supposed to work?
Genky (August 19th, 2012)
-The first line it prints one *, spaces = number row - number of *
- Each line decrease number of * by 2.
Not a good algorithm.
I thought there were spaces before the * on the first line? You steps say to print an * first.The first line it prints one *
If you start with 1 and decrease by 2 there are negative numbers.Each line decrease number of * by 2.
What about the leading spaces on each line before the *s?
write an expression using the line number to compute the number of spaces
and another expression to compute the number of *s on a line
The pseudo code could be:init variables
begin loop1 for number of lines to print
compute number of spaces for this line
print the spaces
compute the number of *s for this line
print the *s
move to next line
end loop1
Does the code work? Does it compile, execute and produce the desired results?
Think this way,
int j=10;
for(int i=1 ; i<10 ; i= i+2) {
...
}
i will give you *,
j-i will give you white space
If you get a compiler error, please copy and paste the full text of the error message here. Otherwise explain what you mean by "it didn't run".It didn't run in this line
Please explain what the problem is. Show the results that the program generates and explain what is wrong with it.And int numsta =i+2 does not bring correct result.
I searched and wrote code for print space like this:
String space = String.format("%" + (n-1) + "s"," ");
But it just prints 4 space. I don't know why.
Please explain what the problem is. Show the results that the program generates and explain what is wrong with it.
I increase each line print *+2 but it wasn't true.
Can you show the programs output and explain what the problem is.I increase each line print *+2 but it wasn't true.
I don't understand what your question is. What is *+2?
Does the value of n change? If it doesn't change then the printf() method call will print the same thing every time.But it just prints 4 space. I don't know why.
This is the out put:
**
***
****
*****
The problems is : The space doesn't show correct.
I mean I've increased number of * each time in for loop = * + 2*.
Please explain what is wrong with the spaces.The space doesn't show correct.
Does that give you the right output?increased number of * each time in for loop = * + 2*.
Last edited by Norm; August 19th, 2012 at 01:22 PM.
Genky (August 20th, 2012)