用星星打印空钻石(*)

时间:2014-01-15 12:55:39

标签: java

钻石应该是这样的:

     *
   *   *
 *       *
   *   * 
     *

我想使用简单的for循环,有人可以帮忙吗? 我试过一颗完整的钻石:

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

4 个答案:

答案 0 :(得分:1)

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

        }
    }
}

答案 1 :(得分:0)

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

需要一堆简单的低级方法:

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

测试:

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

答案 2 :(得分:0)

请为空白钻石尝试此代码:

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

答案 3 :(得分:-1)

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