patternjavaModerate
Java diamond with numbers
Viewed 0 times
withdiamondnumbersjava
Problem
I have an assignment to code a method that will print values in a diamond pattern. For example, if the method is called like
I have spent a ton of time on writing the code that does this, but I am sure that it's not the most efficient/clean way. I am hoping to learn new strategies for accomplishing this, because this was very difficult for me to complete, and I almost just gave up, but I got it to work:
```
public static void printNumberDiamond(int diaCenter)
{
printNumberDiamondTop(diaCenter);
printNumberDiamondBottom(diaCenter);
}
public static void printNumberDiamondTop(int diaCenter)
{
int maxAscendingRows = diaCenter + 1;
int maxColumnLength = diaCenter * 2 + 1;
int highestNbrForRow = 0;
for(int row = 0, rowCtr=0;row= maxAscendingRows)
{
thisColumnMaxLeftSpaces = (maxColumnLength - rowCtr) / 2;
rowCtr--;
}
else
{
thisColumnLength = maxColumnLength - row;
thisColumnMaxLeftSpaces = (thisColumnLength - row) / 2;
rowCtr++;
}
int nbrWritten = 0;
int nbrDescendingWritten = 0;
for(int col = 0;col row)
{
System.out.print(" ");
}
else if(nbrWritten highestNbrForRow)
{
System.out.print(--nbrDescendingWritten);
}
}
++highestNbrForRow;
System.out.print("\n");
}
}
public static void printNumberDiamondBottom(int diaCenter)
{
int maxColumnLength = diaCenter * 2 + 1;
int highestNbrForRow = 0;
for(int row = diaCenter;row>0;row--)
{
highe
printNumberDiamond(2), then the results should look like:" 0 "
" 010 "
"01210"
" 010 "
" 0 "I have spent a ton of time on writing the code that does this, but I am sure that it's not the most efficient/clean way. I am hoping to learn new strategies for accomplishing this, because this was very difficult for me to complete, and I almost just gave up, but I got it to work:
```
public static void printNumberDiamond(int diaCenter)
{
printNumberDiamondTop(diaCenter);
printNumberDiamondBottom(diaCenter);
}
public static void printNumberDiamondTop(int diaCenter)
{
int maxAscendingRows = diaCenter + 1;
int maxColumnLength = diaCenter * 2 + 1;
int highestNbrForRow = 0;
for(int row = 0, rowCtr=0;row= maxAscendingRows)
{
thisColumnMaxLeftSpaces = (maxColumnLength - rowCtr) / 2;
rowCtr--;
}
else
{
thisColumnLength = maxColumnLength - row;
thisColumnMaxLeftSpaces = (thisColumnLength - row) / 2;
rowCtr++;
}
int nbrWritten = 0;
int nbrDescendingWritten = 0;
for(int col = 0;col row)
{
System.out.print(" ");
}
else if(nbrWritten highestNbrForRow)
{
System.out.print(--nbrDescendingWritten);
}
}
++highestNbrForRow;
System.out.print("\n");
}
}
public static void printNumberDiamondBottom(int diaCenter)
{
int maxColumnLength = diaCenter * 2 + 1;
int highestNbrForRow = 0;
for(int row = diaCenter;row>0;row--)
{
highe
Solution
Before going into any coding I would give you a quick hack for the diamond problem . Instead of making so many for looping and columns and keeping track of variables the diamond problem is a variant of a maths problem.
HERE
and so on. SO if you use this formula you just need one for loop for spaces and one for increasing your num*10 +1 where num is 1 and then squaring it and you will get the diamond pattern. If you want I can code it for you but since you asked for an easier method this is the best you can get.
HERE
1*1 = 1
11*11 = 121
111*111 = 12321
1111*1111 = 1234321and so on. SO if you use this formula you just need one for loop for spaces and one for increasing your num*10 +1 where num is 1 and then squaring it and you will get the diamond pattern. If you want I can code it for you but since you asked for an easier method this is the best you can get.
Code Snippets
1*1 = 1
11*11 = 121
111*111 = 12321
1111*1111 = 1234321Context
StackExchange Code Review Q#134184, answer score: 12
Revisions (0)
No revisions yet.