从astericks打印Diamond的Java程序需要调整

时间:2013-10-01 06:02:56

标签: java

我打印出这样的钻石的程序:

...............*
..........*    *    *
.....*    *    *    *    *
*    *    *    *    *    *    *
.....*    *    *    *    *
..........*    *    *    
...............*

但它只有在钻石的参数或每一边都是4时才有效。例如,如果我输入6,底部三角形上的间距是错误的,我一直试图弄明白。

参数更改时底部三角形不会改变,只有顶部三角形改变。它仅适用于输入4。

    public static  void printMoreStars (int n)
{
    int rowsPrime= 0;

    for (int i = n+1; i > 0; i--)
    {
        for (int j = 0; j < (2*i)-1; j++)       
        {
            System.out.print(" ");
        }
        for (int d = 0; d < (2*rowsPrime)-1; d++)
        {
            System.out.print("*" + " ");
        }
        System.out.println();   //new line character

        rowsPrime +=1;
        System.out.println(" ");
    }


    //bottom triangle
    for (int i = 1; i < n+1; i++)
    {
        for (int j = 0; j < (2*i)+1; j++)       
        {
            System.out.print(" ");
        }
        for (int d = 0; d < rowsPrime; d++)
        {
            System.out.print("*" + " ");
        }
        System.out.println();   //new line character

        rowsPrime -=2;
        System.out.println(" ");
    }
}

3 个答案:

答案 0 :(得分:2)

使用rowPrimes时出了两个错误。请参阅下面的注释:

public class Stars {
  public static void main(String[] args) {
    printMoreStars(Integer.parseInt(args[0]));
  }

  public static  void printMoreStars (int n)
  {
    int rowsPrime= 0;

    for (int i = n+1; i > 0; i--)
    {
      for (int j = 0; j < (2*i)-1; j++)  
      {
        System.out.print(" ");
      }
      for (int d = 0; d < (2*rowsPrime)-1; d++)
      {
        System.out.print("*" + " ");
      }
      System.out.println();   //new line character

      rowsPrime +=1;
      System.out.println(" ");
    }

    rowsPrime -= 2; // <- middle line is already printed, so skip that

    //bottom triangle
    for (int i = 1; i < n+1; i++)
    {
      for (int j = 0; j < (2*i)+1; j++)  
      {
        System.out.print(" ");
      }
      for (int d = 0; d < (2*rowsPrime) - 1; d++) // <- changed condition to be the same as above
      {
        System.out.print("*" + " ");
      }
      System.out.println();   //new line character

      rowsPrime--; // <- you have to decrease rowPrime by one.
      System.out.println(" ");
    }
  }
}

答案 1 :(得分:1)

要打印左侧有圆点的菱形,可以使用此代码。

Try it online!

public static void main(String[] args) {
    printRhombus(3);
}
public static void printRhombus(int n) {
    for (int i = -n; i <= n; i++) {
        // last element in the row
        int last = n - Math.abs(i);
        for (int j = -n; j <= last; j++)
            if (Math.abs(i) + Math.abs(j) <= n)
                System.out.print("*" + (j < last ? "    " : ""));
            else
                System.out.print(".....");
        System.out.println();
    }
}

输出:

...............*
..........*    *    *
.....*    *    *    *    *
*    *    *    *    *    *    *
.....*    *    *    *    *
..........*    *    *
...............*

另见:Print a diamond shape with Java

答案 2 :(得分:0)

Check This :-
import java.io.*;

import java.lang.*;

import java.util.*;



class DiamondPattern

{

      static public int ReadInteger()

      {

            try

            {

                  String inpString = "";

                  InputStreamReader input = new InputStreamReader(System.in);

                  BufferedReader reader = new BufferedReader(input);

                  String s = reader.readLine();

                  return Integer.parseInt(s);

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

            return -1;

      }



      public static void main(String[] args)

      {

            System.out.println("Program for displaying pattern of *.");

            System.out.print("Enter the maximum number of *: ");

            int n = ReadInteger();



            System.out.println("\nHere is the Diamond of Stars\n");



            for (int i = 1; i <= n; i++)

            {

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

                        System.out.print(" ");

                  for (int j = 1; j <= i; j++)

                        System.out.print("*");

                  for (int k = 1; k < i; k++)

                        System.out.print("*");

                  System.out.println();

            }



            for (int i = n - 1; i >= 1; i--)

            {

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

                        System.out.print(" ");

                  for (int j = 1; j <= i; j++)

                        System.out.print("*");

                  for (int k = 1; k < i; k++)

                        System.out.print("*");

                  System.out.println();

            }



            System.out.println();

      }

}