I need a triangle like this
Code Java:* *** ***** *******
I just know how to print like this :
*
***
*****
*******
*********
Can you guys give me some hints. Thank you very much.
My code is:
Printable View
I need a triangle like this
Code Java:* *** ***** *******
I just know how to print like this :
*
***
*****
*******
*********
Can you guys give me some hints. Thank you very much.
My code is:
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:
http://www.javaprogrammingforums.com...n-feeding.html
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 *
A space is just another character. Put it inside of some "s:Quote:
how to print spaces
" " for a space
vs
"*" for an *
You mean like thisBut it just print:Code :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?
-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.Quote:
The first line it prints one *
If you start with 1 and decrease by 2 there are negative numbers.Quote:
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:Quote:
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".Quote:
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.Quote:
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.Quote:
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.Quote:
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.Quote:
The space doesn't show correct.
Does that give you the right output?Quote:
increased number of * each time in for loop = * + 2*.