/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/
Символ ?
означает, что предыдущая группа должна совпадать с нулем или один раз. Группа (-|\s)
будет соответствовать символу -
или |
.
Попробуйте следующее:
public static void drawDiamond(int height) {
if (height % 2 == 0) throw new AssertionError("Height should be an odd number!");
height = (height + 1) / 2;
drawTop(height);
drawBot(height - 1);
}
public static void drawTop(int height) {
int rows = height;
int starCount = 1;
int spaceCount = rows - 1;
for (int rowCount = 1; rowCount <= rows; rowCount++) {
for (int i = 0; i < spaceCount; i++) {
System.out.print(" ");
}
for (int i = 0; i < starCount; i++) {
System.out.print("*");
}
starCount += 2;
spaceCount--;
System.out.println();
}
}
public static void drawBot(int height) {
int rows = height;
int starCount = 2 * (rows - 1) + 1;
int spaceCount = 1;
for (int rowCount = 1; rowCount <= rows; rowCount++) {
for (int i = 0; i < spaceCount; i++) {
System.out.print(" ");
}
for (int i = 0; i < starCount; i++) {
System.out.print("*");
}
starCount -= 2;
spaceCount++;
System.out.println();
}
}
Вот еще один угол взгляда на него.
Примечание: высотные нити от середины до самой верхней точки.
public static void DrawDiamond(int height)
{
DiamondTop(height);
DiamondBottom(height);
}
public static void DiamondTop(int height)
{
for (int row = 1; row <= height; row++)
{
for (int padding = height - row; padding > 0; padding--)
{
System.out.print(" ");
}
for (int numberOfAsterisks = (row * 2) - 1; numberOfAsterisks > 0; numberOfAsterisks--)
{
System.out.print("*");
}
System.out.println();
}
}
public static void DiamondBottom(int height)
{
for (int row = height - 1; row > 0; row--)
{
for (int padding = row; padding < height; padding++)
{
System.out.print(" ");
}
for (int numberOfAsterisks = (row * 2) - 1; numberOfAsterisks > 0; numberOfAsterisks--)
{
System.out.print("*");
}
System.out.println();
}
}