Making an ASCII rhombus with loops - java

I got a problem to create a rhombus, my code here:
package random;
public class asd {
public static void main(String args[]) {
for (int j = 1; j <= 4; j++) {
for (int kong = 4 - j; kong >= 1; kong--) {
System.out.print(" ");
}
for (int xing = 1; xing <= 2 * j - 1; xing++) {
System.out.print("*");
}
System.out.println();
}
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= a; b++) {
System.out.print(" ");
}
for (int c = 5; c >= 1; c -= 2) { // <==== here
System.out.print("*");
}
System.out.println();
}
}
}
However, the output is:
*
***
*****
*******
***
***
***
I think the problem is in the code which I highlighted.

java-11
By using String#repeat, introduced as part of Java-11, you can do it with a single loop.
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By increasing the amount of space by one character, you can also print a variant of the shape:
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

You are right in indicating the possible problematic line. Surprised that you did it right in first half:
for (int c = 5; c >= 2 * a - 1; c -= 1) { // <==== here
System.out.print("*");

Using Math.abs will make it a lot easier:
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. " +
"Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" ");
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* ");
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* ");
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}

You can simplify your code by using two nested for loops and one if else statement.
int n = 4;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
See also: Output an ASCII diamond shape using loops

Related

Print an hourglass pattern

I want to print the pattern in java and I have tried the following code. please guide me to solve my mistake.
import java.util.*;
class sp10
{
public static void main(String args[])
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
n = sc.nextInt();
for(i=n;i>=1;i--)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1) ||i==n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for(i=2;i<=n;i++)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1)|| i==n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
I am trying it in Java. Can you please guide me? It should print 5 stars in the first and the last line. Given below is the exact pattern
*****
* *
* *
*
* *
* *
*****
Well, let's look at the pattern.
n = 5
*****
* *
* *
*
* *
* *
*****
The first line and the last line is just print * n times.
private static void printTop(int n) {
System.out.println("*".repeat(n));
}
private static void printBottom(int n) {
printTop(n);
}
Though I wrote two methods here, you can just define one method like printStar(int n) and reuse it on the first and last line.
The real problem is to implement the middle part. Well, this still can be implemented in a relatively simple way. You just need to move * from column 0 to right and from n-1 to left as the row increased.
private static void printMiddle(int n) {
for (int i = 0; i < n; i++) {
int p = i;
for (int j = 0; j < n; j++) {
if (j == p || j == n - 1 - p) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
p is the offset from column 0 to right or column n-1 to left.
Now, you have everything. You can write the main method like the following:
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
int n = sc.nextInt();
printTop(n);
printMiddle(n);
printBottom(n);
}
I suggest the following solution:
import java.util.*;
public class sp10 {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n");
n = sc.nextInt();
System.out.print(" ");
for (i = 0; i < 2*n - 3; i++) {
System.out.print("*");
}
System.out.println("");
for (i = n-1; i >= 1; i--) {
for (j = i; j <n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j==1 || j == (2 * i - 1) || i == n ) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (i = 2; i <= n-1; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || i == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
System.out.print(" ");
for (i = 0; i < 2*n - 3; i++) {
System.out.print("*");
}
}
}
I hope is what you're looking for, because you question was a little bit difficult to understand...
With such solution the output is (for n = 4):
*****
* *
* *
*
* *
* *
*****
Probably, what you intended to do is as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
n = sc.nextInt();
for (i = n; i >= 1; i--) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for (i = 2; i <= n; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || (i == n && j % 2 == 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
A sample run:
Enter value of n: 4
* * * *
* *
* *
*
* *
* *
* * * *
However, if you insist that you need to print exactly what you have shown in the question, you can do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
n = sc.nextInt();
for (i = n; i >= 1; i--) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for (i = 2; i <= n; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
if ((j == 1 && i != n) || (j == (2 * i - 1) && i != n) || (j != 1 && i == n && j != (2 * i - 1)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Output:
Enter value of n: 4
*****
* *
* *
*
* *
* *
*****

Printing a * pattern

So I have to print the following pattern by accepting a value of n
Input : 7
Output has to be this :
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
code:
public static void printPattern(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.println("*");
}
System.out.println("\n");
}
for (int a = n-1; a >= 1; a--)
{
for (int b = 1; b <= a; b++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
But for some reason it prints this pattern(say n=8):
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*******
******
*****
****
***
**
*
What is the mistake here?
Use System.out.print instead of System.out.println in the first for loop, the latter one always appends a newline character at the end which is what you are trying to do manually.
And either do System.out.print("\n"); OR System.out.println(""); to add the newline after the inner loop iteration.
System.out.println already adds a line break at the end, therefore System.out.println("\n") adds two line breaks.
The code can be condensed to a single double-for loop. The following routine accepts a single parameter which defines the maximum number of '*' on a single line:
/**
* #param width
* the maximum width of the pattern.
*/
public static void print(int width) {
for (int i = 1; i < 2 * width; ++i) {
for (int j = 0; j < width - Math.abs(width - i); ++j)
System.out.print("*");
System.out.println();
}
}
public static void main(String[] args) {
print(4);
}
Output:
*
**
***
****
***
**
*
first for loop should be like this,
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
System.out.println();
always move the cursor to a new line
in your code when we use System.out.println("\n"); this will move cursor to 2 lines
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int a = n - 1; a >= 1; a--) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
By changing println to print you won't go to another line after each * and remove the \n to not skip 2 lines because println it's already there

How do you create a tail end on an arrow using sthe string "*"?

THe question might seem confusing, but the gist of it is that I need to make an arrow for, say, n = 3, which would look like
*
**
***
**
*
I want to add a tail to this triangle to make it into an arrow. It has to be length (n-1). So:
*
**
*****
**
*
Here my code so far, which only gets you the triangle. I can't seem to figure out how to get the tail on the arrow. Any help would be great, thanks!
public class Arrow
{
public static void main (String[] args)
{
double n=0;
if (args.length < 1){
System.out.println("Input a number");
System.exit(0); // Terminate the program if user has not input a number
}
else
n = Double.parseDouble(args[0]);
String text = "*";
int n1 = (int) n;
System.out.println(text);
for (int x = 1; x < n1; x++) {
System.out.println(text += "*");
}
for (int i = (n1-1); i>0; i--) {
System.out.println(text = text.substring(0, text.length()-1));
}
}
}
Code like that is much easier to write if you write the repetitive code in a reusable helper method.
That is called the DRY principle (Don't Repeat Yourself).
In this case you want to print repeating characters of '*' for the arrow, and repeating characters of ' ' (space) for the indentation. So, write a method for creating a String of repeated characters:
private static String repeat(char ch, int count) {
char[] buf = new char[count];
Arrays.fill(buf, ch);
return new String(buf);
}
Now that you have that, printing a right-arrow is suddenly a lot easier:
private static void printRightArrow(char ch, int n) {
for (int i = 1; i < n; i++)
System.out.println(repeat(' ', n - 1) + repeat(ch, i));
System.out.println(repeat(ch, n * 2 - 1));
for (int i = n - 1; i >= 1; i--)
System.out.println(repeat(' ', n - 1) + repeat(ch, i));
}
Test and Output
printRightArrow('*', 3);
printRightArrow('#', 7);
*
**
*****
**
*
#
##
###
####
#####
######
#############
######
#####
####
###
##
#
Printing arrows in the other direction is very similar:
private static void printLeftArrow(char ch, int n) {
for (int i = 1; i < n; i++)
System.out.println(repeat(' ', n - i) + repeat(ch, i));
System.out.println(repeat(ch, n * 2 - 1));
for (int i = n - 1; i >= 1; i--)
System.out.println(repeat(' ', n - i) + repeat(ch, i));
}
private static void printUpArrow(char ch, int n) {
for (int i = 1; i <= n; i++)
System.out.println(repeat(' ', n - i) + repeat(ch, i * 2 - 1));
for (int i = 1; i < n; i++)
System.out.println(repeat(' ', n - 1) + ch);
}
private static void printDownArrow(char ch, int n) {
for (int i = 1; i < n; i++)
System.out.println(repeat(' ', n - 1) + ch);
for (int i = n; i >= 1; i--)
System.out.println(repeat(' ', n - i) + repeat(ch, i * 2 - 1));
}
Test and Output
printRightArrow('*', 5);
printLeftArrow('*', 5);
printUpArrow('*', 5);
printDownArrow('*', 5);
*
**
***
****
*********
****
***
**
*
*
**
***
****
*********
****
***
**
*
*
***
*****
*******
*********
*
*
*
*
*
*
*
*
*********
*******
*****
***
*
This is definitely not as robust as Andreas' solution, but it is much like yours. I have just added some for loops and if-else statements.
class Arrow {
public static void main (String[] args) {
double n = 0;
if (args.length < 1){
System.out.println("Input a number");
System.exit(0); // Terminate the program if user has not input a number
}
else
n = Double.parseDouble(args[0]);
String text = "";
int n1 = (int) n;
for (int x = 0; x < n1; x++) {
for (int i = 0; i < n1 - 1; i++) {
if (x != n1 - 1) {
System.out.print(" ");
}
else {
System.out.print("*");
}
}
System.out.println(text += "*");
}
for (int i = (n1-1); i>0; i--) {
for (int j = (n1 - 1); j > 0; j--) {
System.out.print(" ");
}
System.out.println(text = text.substring(0, text.length()-1));
}
}
}

Pattern using looping

I have to make a loop that will read the user's input (let's say his input is 5) so the output should be as the following:-
*
**
***
****
*****
****
***
**
*
int x;
//input size of triangle from 1-20
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle from 1 to 20: ");
x = scanner.nextInt ();
for( int i = 0; i <= x; i++){
for(int j=0; j < i; j++){
System.out.print( " *");
}
System.out.println(" ");
I just don't know how to finish it by decreasing the pattern so I can form a full triangle.
example using ternary operator .increase j if x grater than i decrease otherwise
int x = 5, j=0;
for (int i = 0; i <= x*2; i++) {
j= (x>i)? ++j:--j; // u can use if else also
for (int y = 0; y < j; y++) {
System.out.print(" *");
}
System.out.println("");
}
output>>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The following code prints the triangle, assuming the user enters 10
for (int i = 0; i < 10; i++){
for (int j = 0;j < i;j++ )
System.out.print('x');
System.out.println();
}
for (int i = 10; i > 0; i--){
for (int j = 0; j < i;j++)
System.out.print('x');
System.out.println();
}
In your code, you exclude the second for loop in which i is decremented.
import java.util.Scanner;
public class Triangle {
public static void printLine(int n) {
for(int i = 0; i < n; i++) System.out.print("*");
System.out.print("\n");
}
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle");
int x = scanner.nextInt ();
for(int i = 0; i < x; i++) {
printLine(i);
}
for(int i = x; i > 0; i--) {
printLine(i);
}
}
}
The output :
Enter size of triangle
5
*
**
***
****
*****
****
***
**
*

create border only triangle using Java loops

Hi there i want to create a triangle using java loops. But what i need is only the borders. I've tried using this syntax but it always shows error. any advice?
int lines = 5;
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
{
if (k == 0 || k == lines - 1) {
System.out.print("*");
}
}
}
System.out.print("\n");
c -= 2;
}
can you kindly help me . thanks
There's a lot of things wrong with your code...
for (int k = 1; k <= c; k++)
// and then:
if (k == 0 || k == lines - 1) {
k will never be 0
if (k % 2 == 0)
// and then
else
{
if (k == 0 || k == lines - 1) {
a even if k was 0, then 0%2 == 0, so k==0 can never occur
As of right now, your app only prints spaces.
If you want to draw a triangle, the (possibly) simplest way would be to do
public static void main(String[] args) {
int lines = 6;
int cols = lines+1;
for (int line =0; line < lines-1; line++) {
for (int col = 0; col<cols; col++) {
if (col == 0 || col == line) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int col=0; col<cols; col++) {
System.out.print("*");
}
System.out.println();
}
Triangle.java
public class Triangle {
private int lines;
public Triangle(int lines) {
this.lines = lines;
display();
}
public void display() {
int spaces = lines - 1;
int last;
int min = 1;
int max;
for (int i = 0; i < lines; i++) {
last = 2 * i + 1;
max = spaces + last;
for (int j = min; j <= max; j++) {
if (j == spaces + 1 || j == max || i == lines - 1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.println();
spaces--;
}
}
public static void main(String[] args) {
new Triangle(3);
new Triangle(4);
new Triangle(5);
new Triangle(6);
}
}
Output
*
* *
*****
*
* *
* *
*******
*
* *
* *
* *
*********
*
* *
* *
* *
* *
***********
Ex:
*
* *
* *
* *
*********
Code:
int numberOfRows = 5;
for(int i=0; i<numberOfRows; i++)
{
for(int a=0; a<=numberOfRows; a++)
{
int x = numberOfRows - i;
if (a > x) {
if (x==(a-1) || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
for(int b=0; b<=i; b++)
{
if(b==i || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
I have used two methods and repeated those two methods using for loop.
public class OpenWord {
int i;
String k = " ";
public void space(){
System.out.println("*"+k+"*");
}
public void star(){
System.out.println("*");
}
public void trainagle(){
star();
for ( i=1; i<=10;i++){
space();
k =k+" ";
if(i==10){
k=k.replace(" ", "*");
space();
}
}
}
public static void main(String args[]) {
OpenWord a = new OpenWord();
a.trainagle();
}
}
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
******************

Categories

Resources