Diamond with nested for loop in Java

2019-03-05 18:22发布

I am trying to display a diamond of asterisks using nested for loops.

Here is my code so far:

    public class Diamond {

        public static void main(String[] args) {

            int size = 9;

            for (int i = 1; i <= size; i += 2) {
                for (int k = size; k >= i; k -= 2) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= i; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }// end loop

            for (int i = 1; i <= size; i += 2) {
               for (int k = 1; k <= i; k += 2) {
                    System.out.print(" ");
                }
                for (int j = size; j >= i; j--) {
                    System.out.print("*");
                }
                System.out.println();
            }// end loop
        }

    }

This is close but I'm printing the line of 9 asterisks twice.

How can I adjust the second for loop to start the output at 7 asterisks and 2 spaces??

Thanks for the help!

5条回答
乱世女痞
2楼-- · 2019-03-05 18:47

Try this code. Using Math.abs will be a lot simple.

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();
    }
    }
  }
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-05 18:48

In your first for loop remove = mark and just use < e.g. for (int i = 1; i < size; i += 2)

Full code

    int size = 9;

    for (int i = 1; i < size; i += 2) {
        for (int k = size; k >= i; k -= 2) {
            System.out.print(" ");
        }
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }// end loop

    for (int i = 1; i <= size; i += 2) {
        for (int k = 1; k <= i; k += 2) {
            System.out.print(" ");
        }
        for (int j = size; j >= i; j--) {
            System.out.print("*");
        }
        System.out.println();
    }// end loop
查看更多
爷的心禁止访问
4楼-- · 2019-03-05 18:55

Try this code:

I have changed the first loop:

    for (int i = 1; i <= size-1; i += 2) {

    int size = 9;

    for (int i = 1; i <= size-1; i += 2) {
        for (int k = size; k >= i; k -= 2) {
            System.out.print(" ");
        }
        for (int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }// end loop

    for (int i = 1; i <= size; i += 2) {
       for (int k = 1; k <= i; k += 2) {
            System.out.print(" ");
        }
        for (int j = size; j >= i; j--) {
            System.out.print("*");
        }
        System.out.println();
    }// end loop
}
查看更多
唯我独甜
5楼-- · 2019-03-05 18:58
        int n = 9;
        for(int i =0;i<n;i++){
            for(int k=n-1;k>i;k--){
                System.out.print(" ");
            }
            for(int j=0;j<2*i+1;j++){
                System.out.print("*");
            }
            System.out.println("");
        }

        for(int j=0;j<n-1;j++){

            for(int k=j;k>=0;k--){
                System.out.print(" ");
            }

            for(int i=2*(n-j-1)-1;i>0;i--){
                System.out.print("*");
            }

            System.out.println("");

        }
查看更多
干净又极端
6楼-- · 2019-03-05 18:58

Just for fun... :) try my code....

public class Diamond {

    static String sp(int n) {
        String s = "";
        for (int i = 0; i < n; i++)
            s += " ";
        return s;
    }
    static String st(int n) {
        String s = "";
        for (int i = 0; i < n; i++)
            s += "*";
        return s;
    }
    static int abs(int n) {
        if (n < 0)
            return -n;
        else
            return n;
    }

    public static void main(String[] args) {

        int size = 9;
        for (int i = 0; i < size; i++) {
            System.out.println(sp(abs((size-1)/2-i)) +
                               st(abs(9-2*((i+5)%(size))))  +
                               sp(abs((size-1)/2-i)));
        }
    }
}
查看更多
登录 后发表回答