Diamond with nested for loop in Java

瘦欲@ 提交于 2019-12-02 15:57:44

问题


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!


回答1:


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



回答2:


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
}



回答3:


        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("");

        }



回答4:


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)));
        }
    }
}



回答5:


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();
    }
    }
  }
}


来源:https://stackoverflow.com/questions/25712029/diamond-with-nested-for-loop-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!