Frage

A diamond should be like this:

    *
  *   *
*       *
  *   *
    *

I want to use simple for loop for it can somebody help? I have tried a complete diamond:

void main() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("ENTER THE HEIGHT ");
    int ht = Integer.parseInt(br.readLine());
    int ht2 = (int) Math.floor(ht / 2);
    ht = (int) Math.ceil(ht / 2);
    for (int ht3 = ht - 1; ht3 >= 0; ht3--) {
        for (int i = 1; i <= ht3; i++)
            System.out.print(" ");
        for (int j = 1; j <= ht - ht3; j++)
            System.out.print("* ");
        System.out.println();
    }
    for (int ht3 = 1; ht3 <= ht2; ht3++) {
        for (int i = 1; i <= ht3; i++)
            System.out.print(" ");
        for (int j = ht2 - ht3; j >= 1; j--)
            System.out.print("* ");
        System.out.println();
    }
}
War es hilfreich?

Lösung 4

import java.io.*;
public class EmptyDiamond {
    void main() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ENTER THE HEIGHT ");
        int ht = Integer.parseInt(br.readLine());
        int space = ht - 1;
        int midspace = -1;
        int k;
        for (int c = 1; c <= Math.ceil(ht / 2); ) {
            for (int i = space; i >= 1; i -= 2) {
                int i2 = i;
                while (i2 != 0) {
                    System.out.print(" ");
                    i2--;
                }
                System.out.print("*");
                for (int j = 1; j <= midspace; j++)
                    System.out.print(" ");
                if (c != 1)
                    System.out.print("*");
                midspace += 4;
                System.out.println();
                c++;
            }
        }
        for (int c = 1; c <= Math.floor(ht / 2); ) {
            for (int i = 0; i <= space; i += 2) {
                int i2 = i;
                while (i2 != 0) {
                    System.out.print(" ");
                    i2--;
                }
                midspace -= 4;
                System.out.print("*");
                for (k = midspace + 4; k >= 1; k--)
                    System.out.print(" ");
                if ((c != (ht / 2 + 1)))
                    System.out.print("*");
                System.out.println();
                c++;
            }
        }
    }
}

Andere Tipps

public class EmptyDiamond {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ENTER THE HEIGHT ");
        int ht = Integer.parseInt(br.readLine());
        int space = ht - 1;
        int midspace = -1;
        for (int i = 0; i < space; i += 2) {
            // start
            for (int j = i; j < space; j++) {
                System.out.print(" ");
            }
            // star
            System.out.print("*");
            // middle
            for (int j = 1; j <= midspace; j++)
                System.out.print(" ");
            midspace += 4;
            // star
            if (i != 0)
                System.out.print("*");
            System.out.println();
        }
        for (int i = 0; i <= space; i += 2) {
            // start
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            // star
            System.out.print("*");
            // middle
            for (int k = 0; k < midspace; k++)
                System.out.print(" ");
            midspace -= 4;
            // star
            if (i != space)
                System.out.print("*");
            System.out.println();
        }
    }
}

Please try this code for empty diamond:

import java.util.*;
class Pattern {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int a, b;
        System.out.println("Please enter the number of rows : ");
        a = sc.nextInt();
        for (int i = 1; i <= a; i++) {
            int l = a;
            while (l > i) {
                System.out.print(" ");
                l--;
            }
            int k = 0;
            if (i <= 2) {
                while (k != i) {
                    System.out.print(" *");
                    k++;
                }
            } else {
                System.out.print(" *");
                int m = 1;
                while (m < i) {
                    System.out.print("  ");
                    m++;
                }
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = a - 1; i >= 1; i--) {
            int l = a - i;
            while (l != 0) {
                System.out.print(" ");
                l--;
            }
            int k = 0;
            if (!(i > 2)) {
                if (i == 2) {
                    System.out.print(" *  *");
                }
                if (i == 1) {
                    System.out.print(" *");
                }
            } else {
                int g = 0;
                System.out.print("*");
                while (g < i) {
                    System.out.print("  ");
                    g++;
                }
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
public static void diamond(int height) {
    for (int i = 0; i <= height; i++) {
        int edge1 = mod((height / 2 - i), height);
        int edge2 = mod((height / 2 + i), height);
        int left = Math.min(edge1, edge2);
        int right = Math.max(edge1, edge2);
        if (i == height / 2 && height % 2 == 1) {
            continue;
        }
        spaces(left);
        diamond();
        if (right != left) {
            spaces(right - left);
            diamond();
        }
        newLine();
    }
}

A bunch of simple lower-level methods is needed:

private static int mod(int a, int b) {
    int c = a % b;
    return (c < 0) ? c + b : c;
}

private static void spaces(int count) {
    for (int i = 0; i < count; i++) {
        System.out.print(" ");
    }
}

private static void diamond() {
    System.out.print("*");
}

private static void newLine() {
    System.out.println();
}

To test:

public static void main(String[] args) {
    diamond(5);
}

Output:

  *
 *  *
*    *
 *  *
  *
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top