implement generateTriangle method in MainExercises
This commit is contained in:
@@ -1,28 +1,26 @@
|
|||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
|
||||||
the triangle's area is empty, which means some characters should be " "
|
|
||||||
|
|
||||||
example 1, input = 3:
|
|
||||||
*
|
|
||||||
**
|
|
||||||
***
|
|
||||||
|
|
||||||
example 2, input = 5:
|
|
||||||
*
|
|
||||||
**
|
|
||||||
* *
|
|
||||||
* *
|
|
||||||
*****
|
|
||||||
|
|
||||||
the output has to be a two-dimensional array of characters, so don't just print the triangle!
|
|
||||||
*/
|
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
|
||||||
// todo
|
char[][] result = new char[n][n];
|
||||||
return null;
|
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
if (i == n-1)
|
||||||
|
{
|
||||||
|
for (int j = 0; j <= i; j++)
|
||||||
|
{
|
||||||
|
result[i][j] = '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result[i][0] = '*';
|
||||||
|
result[i][i] = '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user