How to make this kind of custom algorithm in Java? [closed]
I am enhancing my Java skills for my own personal purpose,
And I am wondering, how to make this algorithm in Java :
* 2 3 4 5
* * 3 4 5
* * * 4 5
* * * * 5
* * * * *
* * * * *
* * * * 5
* * * 4 5
* * 3 4 5
* 2 3 4 5
So far from now I managed to do this :
*
**
***
****
*****
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println(" ");
}
And I wanted to do something harder.
java algorithm
closed as too broad by Stephen C, meowgoesthedog, cнŝdk, Dukeling, JJJ Nov 20 '18 at 14:02
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am enhancing my Java skills for my own personal purpose,
And I am wondering, how to make this algorithm in Java :
* 2 3 4 5
* * 3 4 5
* * * 4 5
* * * * 5
* * * * *
* * * * *
* * * * 5
* * * 4 5
* * 3 4 5
* 2 3 4 5
So far from now I managed to do this :
*
**
***
****
*****
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println(" ");
}
And I wanted to do something harder.
java algorithm
closed as too broad by Stephen C, meowgoesthedog, cнŝdk, Dukeling, JJJ Nov 20 '18 at 14:02
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
10
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Hint: one way to do this is to change the inner loop to always run to 5, and use anif
inside. Another option is to use a second, separate inner loop for the numbers.
– Hulk
Nov 20 '18 at 11:55
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
I am enhancing my Java skills for my own personal purpose,
And I am wondering, how to make this algorithm in Java :
* 2 3 4 5
* * 3 4 5
* * * 4 5
* * * * 5
* * * * *
* * * * *
* * * * 5
* * * 4 5
* * 3 4 5
* 2 3 4 5
So far from now I managed to do this :
*
**
***
****
*****
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println(" ");
}
And I wanted to do something harder.
java algorithm
I am enhancing my Java skills for my own personal purpose,
And I am wondering, how to make this algorithm in Java :
* 2 3 4 5
* * 3 4 5
* * * 4 5
* * * * 5
* * * * *
* * * * *
* * * * 5
* * * 4 5
* * 3 4 5
* 2 3 4 5
So far from now I managed to do this :
*
**
***
****
*****
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println(" ");
}
And I wanted to do something harder.
java algorithm
java algorithm
edited Nov 20 '18 at 12:22
meowgoesthedog
9,99241527
9,99241527
asked Nov 20 '18 at 11:48
Finalmix6Finalmix6
887
887
closed as too broad by Stephen C, meowgoesthedog, cнŝdk, Dukeling, JJJ Nov 20 '18 at 14:02
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Stephen C, meowgoesthedog, cнŝdk, Dukeling, JJJ Nov 20 '18 at 14:02
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
10
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Hint: one way to do this is to change the inner loop to always run to 5, and use anif
inside. Another option is to use a second, separate inner loop for the numbers.
– Hulk
Nov 20 '18 at 11:55
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
10
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Hint: one way to do this is to change the inner loop to always run to 5, and use anif
inside. Another option is to use a second, separate inner loop for the numbers.
– Hulk
Nov 20 '18 at 11:55
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12
10
10
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Hint: one way to do this is to change the inner loop to always run to 5, and use an
if
inside. Another option is to use a second, separate inner loop for the numbers.– Hulk
Nov 20 '18 at 11:55
Hint: one way to do this is to change the inner loop to always run to 5, and use an
if
inside. Another option is to use a second, separate inner loop for the numbers.– Hulk
Nov 20 '18 at 11:55
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
3 Answers
3
active
oldest
votes
This should be of some help:
public static void printTree()
{
int index = 0;
for(; index < 5; index++)
{
for(int j = 0; j < 5; j++)
{
if(j <= index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
for(; index > 0; index--)
{
for(int j = 0; j < 5; j++)
{
if(j < index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
}
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
add a comment |
The way to program is to break the problem up into smaller chunks and work on one chunk at a time. Always test each chunk before moving on to the next.
Make rows of stars getting longer. You have done this.
Make rows of stars getting shorter.
Make rows of digits, increasing.
Make rows of digits, decreasing.
Append the decreasing digits to the increasing stars.
Append the increasing digits to the decreasing stars.
Put 5 and 6 together.
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
for (int i = 5; i > 0 ; i--) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
I don't know if it is the best way, but it seems to work.
I will try another way. Always good to learn.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This should be of some help:
public static void printTree()
{
int index = 0;
for(; index < 5; index++)
{
for(int j = 0; j < 5; j++)
{
if(j <= index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
for(; index > 0; index--)
{
for(int j = 0; j < 5; j++)
{
if(j < index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
}
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
add a comment |
This should be of some help:
public static void printTree()
{
int index = 0;
for(; index < 5; index++)
{
for(int j = 0; j < 5; j++)
{
if(j <= index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
for(; index > 0; index--)
{
for(int j = 0; j < 5; j++)
{
if(j < index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
}
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
add a comment |
This should be of some help:
public static void printTree()
{
int index = 0;
for(; index < 5; index++)
{
for(int j = 0; j < 5; j++)
{
if(j <= index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
for(; index > 0; index--)
{
for(int j = 0; j < 5; j++)
{
if(j < index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
}
This should be of some help:
public static void printTree()
{
int index = 0;
for(; index < 5; index++)
{
for(int j = 0; j < 5; j++)
{
if(j <= index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
for(; index > 0; index--)
{
for(int j = 0; j < 5; j++)
{
if(j < index)
{
System.out.print("*");
}
else
{
System.out.print(j + 1);
}
}
System.out.println();
}
}
answered Nov 20 '18 at 12:25
Karan KhannaKaran Khanna
633628
633628
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
add a comment |
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
1
1
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
Oh your solution is even better than mine ! Thank you, I will learn if there is some others ways to do it.
– Finalmix6
Nov 20 '18 at 12:28
add a comment |
The way to program is to break the problem up into smaller chunks and work on one chunk at a time. Always test each chunk before moving on to the next.
Make rows of stars getting longer. You have done this.
Make rows of stars getting shorter.
Make rows of digits, increasing.
Make rows of digits, decreasing.
Append the decreasing digits to the increasing stars.
Append the increasing digits to the decreasing stars.
Put 5 and 6 together.
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
The way to program is to break the problem up into smaller chunks and work on one chunk at a time. Always test each chunk before moving on to the next.
Make rows of stars getting longer. You have done this.
Make rows of stars getting shorter.
Make rows of digits, increasing.
Make rows of digits, decreasing.
Append the decreasing digits to the increasing stars.
Append the increasing digits to the decreasing stars.
Put 5 and 6 together.
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
The way to program is to break the problem up into smaller chunks and work on one chunk at a time. Always test each chunk before moving on to the next.
Make rows of stars getting longer. You have done this.
Make rows of stars getting shorter.
Make rows of digits, increasing.
Make rows of digits, decreasing.
Append the decreasing digits to the increasing stars.
Append the increasing digits to the decreasing stars.
Put 5 and 6 together.
The way to program is to break the problem up into smaller chunks and work on one chunk at a time. Always test each chunk before moving on to the next.
Make rows of stars getting longer. You have done this.
Make rows of stars getting shorter.
Make rows of digits, increasing.
Make rows of digits, decreasing.
Append the decreasing digits to the increasing stars.
Append the increasing digits to the decreasing stars.
Put 5 and 6 together.
answered Nov 20 '18 at 12:01
rossumrossum
12.6k11431
12.6k11431
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
Thank you for these tips, I'm working on it.
– Finalmix6
Nov 20 '18 at 12:12
add a comment |
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
for (int i = 5; i > 0 ; i--) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
I don't know if it is the best way, but it seems to work.
I will try another way. Always good to learn.
add a comment |
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
for (int i = 5; i > 0 ; i--) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
I don't know if it is the best way, but it seems to work.
I will try another way. Always good to learn.
add a comment |
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
for (int i = 5; i > 0 ; i--) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
I don't know if it is the best way, but it seems to work.
I will try another way. Always good to learn.
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
for (int i = 5; i > 0 ; i--) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
for (int k = i+1; k <= 5 ; k++) {
System.out.print(k);
}
System.out.println(" ");
}
I don't know if it is the best way, but it seems to work.
I will try another way. Always good to learn.
edited Nov 20 '18 at 12:29
answered Nov 20 '18 at 12:22
Finalmix6Finalmix6
887
887
add a comment |
add a comment |
10
Solution: keep working at it. That's the best way to learn how to program. Practice.
– Stephen C
Nov 20 '18 at 11:50
Hint: one way to do this is to change the inner loop to always run to 5, and use an
if
inside. Another option is to use a second, separate inner loop for the numbers.– Hulk
Nov 20 '18 at 11:55
I prefer the second inner loop, @Hulk. It will be easier to read even when calculating the loop start and end points is not quite so trivial.
– Ole V.V.
Nov 20 '18 at 12:03
You can use recursion to build the lower half. When the call returns back, print the same line again and it's done.
– vivek_23
Nov 20 '18 at 12:07
Ok thanks, I'm still working on it.
– Finalmix6
Nov 20 '18 at 12:12