Where/how to add space in between asterisks,
this is what i've done so far..
int i, j, k;
int n = 5;
for (i = 1; i <= n; i++) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("*");
}
for (k = 1; k < i; k++) {
System.out.print("*");
}
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("*");
}
for (k = 1; k < i; k++) {
System.out.print("*");
}
System.out.print("\n");
}
Your actual output is this:
*
***
*****
*******
*********
*******
*****
***
*
Where/how to add space in between asterisks?
If your expected output is this:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Use an extra space in each print statement
int i, j, k;
int n = 5;
for (i = 1; i <= n; i++) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
for (k = 1; k < i; k++) {
System.out.print("* ");
}
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--) {
for (j = 0; j < (n - i); j++) {
System.out.print(" ");
}
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
for (k = 1; k < i; k++) {
System.out.print("* ");
}
System.out.print("\n");
}
Check here a working demo
As long as I understand your question correctly, you should be able to replace "**" with "* *" if you want to " add space in between asterisks". Something like this:
String asterisks = "**";
asterisks = asterisks.replaceAll("\\*\\*, \\* \\*);
Related
I am attempting to print a diamond using a user inputted width of 7 that is supposed to look like this:
*
* *
* *
* *
* *
* *
*
BUT, unfortunately my diamond is messed up on the bottom and it looks like this:
*
* *
* *
* *
* *
*
Here's my code:
public static void main (String[] args)
{
int width = 0;
System.out.println("What is the width?");
Scanner keyboard = new Scanner (System.in);
width = keyboard.nextInt();
//Print top half of the diamond
for (int i = 0; i < width; i += 2) {
if (i == 0) {
for (int j = 0; j < (width / 2) + 1; j++)
System.out.print(' ');
System.out.print('*');
} else {
for (int j = 0; j < width - i; j += 2) {
System.out.print(' ');
}
System.out.print('*');
for (int j = 0; j < i - 1; j++) {
System.out.print(' ');
}
System.out.print('*');
}
System.out.print('\n');
}
//Print bottom half of the diamond
for (int i = 0; i < 3; i+=2) {
if (i == 2) {
for (int j = 0; j < (width / 2) + 1; j++)
System.out.print(' ');
System.out.print('*');
} else {
for (int j = 0; j <= i + 2; j += 2) {
System.out.print(' ');
} System.out.print ('*');
for (int j = 0; j < (width / 2) - i; j++) {
System.out.print(' ');
} System.out.print('*');
}
System.out.print('\n');
}
}
I am not totally sure how to fix the bottom part of it, that is what I have been trying to figure out! These nested for loops are tricky
For simple fix, change bottom loop variable i from 3 to 5, and line after that i from 2 to 4.
//Print bottom half of the diamond
for (int i = 0; i < 5; i+=2) {
if (i == 4) {
for (int j = 0; j < (width / 2) + 1; j++)
System.out.print(' ');
System.out.print('*');
} else {
for (int j = 0; j <= i + 2; j += 2) {
System.out.print(' ');
} System.out.print ('*');
for (int j = 0; j < (width / 2) - i; j++) {
System.out.print(' ');
} System.out.print('*');
}
System.out.print('\n');
}
Try changing the 3 in bottom half for loop to 4. It seems as you lack one row. That might do it. Can't promise I'm new here and just trying to help! :)
I need to write a program that will print the following output
*
* * *
* * * *
but my code till now is printing instead this
*
* *
* * *
My code is:
public class Pyramid2 {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Looks little weird, but just do not run the loop for value of i = 1, ideally pyramid is always 1, 2 , 3... so on structure type...
public static void main(String[] args) {
for (int i = 0 ; i < 4 ; i++) {
if (i != 1) {
for (int j = 0 ; j < 4 - i ; j++) {
System.out.print(" ");
}
for (int k = 0 ; k <= i ; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output
*
* * *
* * * *
I agree that there might be an error in the assignment, and that three separate print statements would be a perfectly good way of executing it.
Seems you could also slip in an if statement to exclude the line with two stars from the loop.
Please check the Lower Piramid Part, I was doing for some other answer,But that one is closed now. So check below once hope it may help
// * * * * * * *
// * * * * *
// * * *
// *
// * * *
// * * * * *
// * * * * * * *
// Insert input for Total Use only odd Number
int total = 11, t = 0;
// Upper Piramid
for (int i = 0; i < total / 2; i++) {
t = 0;
for (int k = i; k > 0; k--) {
System.out.print(" ");
t++;
}
for (int j = 0; j < total - (t * 2); j++) {
System.out.print("*");
}
for (int j = 0; j < t; j++) {
System.out.print(" ");
}
System.out.println("");
}
total = total - (total / 2);
// Lower Piramid
for (int i = total; i > 0; i--) {
t = 0;
for (int j = i; j > 1; j--) {
System.out.print(" ");
t++;
}
for (int j = 0; j < total - t; j++) {
System.out.print("*");
}
for (int j = 1; j < total - t; j++) {
System.out.print("*");
}
for (int j = i; j > 1; j--) {
System.out.print(" ");
}
System.out.println("");
}
I have this code, which I am trying to print the following shape out...
****
* *
**
*
Current Code:
System.out.print("\nEnter number of rows: ");
rows = kybd.nextInt();
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == rows - 1 || j == i) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
For some reason, it prints out like this:
****
*
**
* *
Any idea how to reverse the second for loop, so it prints the other way?
I feel you should change your i loop
for (int i = rows - 1; i > 0 ; i--)
Try this:
Scanner kybd = new Scanner(System.in);
System.out.print("\nEnter number of rows: ");
int rows = kybd.nextInt();
if (rows > 1) {
for (int i = 0; i < rows; i++)
System.out.print("*");
System.out.println();
for (int i = rows - 1; i > 1; i--) {
System.out.print("*");
for (int j = 2; j < i; j++)
System.out.print(" ");
System.out.println("*");
}
}
System.out.println("*");
Sample output:
Enter number of rows: 6
******
* *
* *
* *
**
*
Notice, that you are printing out the first line of asterisk in a separate cycle, not inside the main one. If you want to follow this road, you need to put the first cycle behind the second one.
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if ((j == 1) || (j == (rows - 1)) || (j == i)) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
My program that prints out a diamond like this:
...............*
..........* * *
.....* * * * *
* * * * * * *
.....* * * * *
..........* * *
...............*
But it only works if the parameter or each side of the diamond is 4. For example if I input 6, the spacing on the bottom triangle is wrong and I been trying to figure it out.
The bottom triangle doesn't change when the parameter is changed, only the top one does. It only works for input 4.
public static void printMoreStars(int n) {
int rowsPrime = 0;
for (int i = n + 1; i > 0; i--) {
for (int j = 0; j < (2 * i) - 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime += 1;
System.out.println(" ");
}
//bottom triangle
for (int i = 1; i < n + 1; i++) {
for (int j = 0; j < (2 * i) + 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < rowsPrime; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime -= 2;
System.out.println(" ");
}
}
You made two mistakes when using rowPrimes. See my annotations below:
public class Stars {
public static void main(String[] args) {
printMoreStars(Integer.parseInt(args[0]));
}
public static void printMoreStars(int n) {
int rowsPrime = 0;
for (int i = n + 1; i > 0; i--) {
for (int j = 0; j < (2 * i) - 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime += 1;
System.out.println(" ");
}
rowsPrime -= 2; // <- middle line is already printed, so skip that
//bottom triangle
for (int i = 1; i < n + 1; i++) {
for (int j = 0; j < (2 * i) + 1; j++) {
System.out.print(" ");
}
for (int d = 0; d < (2 * rowsPrime) - 1; d++) { // <- changed condition to be the same as above
System.out.print("*" + " ");
}
System.out.println(); //new line character
rowsPrime--; // <- you have to decrease rowPrime by one.
System.out.println(" ");
}
}
}
To print a rhombus with dots on the left, you can use this code.
Try it online!
public static void main(String[] args) {
printRhombus(3);
}
public static void printRhombus(int n) {
for (int i = -n; i <= n; i++) {
// last element in the row
int last = n - Math.abs(i);
for (int j = -n; j <= last; j++)
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*" + (j < last ? " " : ""));
else
System.out.print(".....");
System.out.println();
}
}
Output:
...............*
..........* * *
.....* * * * *
* * * * * * *
.....* * * * *
..........* * *
...............*
See also: Print a diamond shape with Java
Check This:
import java.io.*;
import java.lang.*;
import java.util.*;
class DiamondPattern {
static public int ReadInteger() {
try {
String inpString = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String s = reader.readLine();
return Integer.parseInt(s);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public static void main(String[] args) {
System.out.println("Program for displaying pattern of *.");
System.out.print("Enter the maximum number of *: ");
int n = ReadInteger();
System.out.println("\nHere is the Diamond of Stars\n");
for (int i = 1; i <= n; i++) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}
I am trying to make the triangle I have made up side down.
Tried many times, but I don't know how to do this.
The code I have know is:
public static void drawPyramide(int lines, char symbol, boolean startDown) {
//TRIANGLE
if(startDown) {
//The triangle up side down should be here.
}
else {
int c = 1;
for (int i = 0; i < lines; i++) {
for (int j = i; j < lines; j++) {
System.out.print(" ");
}
for (int k = 1; k <= c; k++) {
if (k%2==0) System.out.print(" ");
else System.out.print(symbol);
}
System.out.print("\n");
c += 2;
}
}
}
Any suggestions how I can "flip" this triangle? Thanks.
To flip the triangle you really just need to change the direction of iteration. Instead of going from i = 0 to i < lines you need to go down from i = lines-1 to i >= 0
You also need to change the c to how many spaces and symbols you want to start with.
Could look like this:
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print(symbol);
}
}
System.out.print("\n");
c -= 2;
}
Reverse the first loop condition i.e. start from number of line and decrease it. Also adjust you c accordingly and make it reduce from high to low e.g. below:
int c = 2*lines-1;
for (int i = lines; i > 0; i--) {
for (int j = i; j < lines; j++) {
System.out.print(" ");
}
for (int k = 1; k <= c; k++) {
if (k%2==0) System.out.print(" ");
else System.out.print(symbol);
}
System.out.print("\n");
c -= 2;
}
import java.util.Scanner;
public class EquilateralTraingle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int side = sc.nextInt();
constructEquTri(side);
}
private static void constructEquTri(int length) {
// loop for each line
for (int i = length; i > 0; i--) {
// loop for initial spaces in each line
for (int k = 0; k < length - i; k++) {
System.out.print(" ");
}
// loop for printing * in each line
for (int j = 0; j < i; j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
/*Output:
10
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*/