2

所以我被分配用Java制作带有星号的钻石,我真的很难过。到目前为止,这是我想出的:

public class Lab1 {
    public static void main(String[] args) {
        for (int i = 5; i > -5; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j >= i; j--) {
                System.out.print(" ");
            }
            System.out.println("*");
        }
    }
}

输出

4

19 回答 19

4

为了制作钻石,您需要设置空间和星星的形状。因为我是初学者,所以我只使用嵌套循环制作了这个简单的程序。

public class Diamond {
    public static void main(String[] args) {
        int size = 9,odd = 1, nos = size/2; // nos =number of spaces
        for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
            for (int k = nos; k >= 1; k--) { // for number of spaces i.e
                                                // 3,2,1,0,1,2,3 and so on
                System.out.print(" ");
            }
            for (int j = 1; j <= odd; j++) { // for number of columns i.e
                                                // 1,3,5,7,5,3,1
                System.out.print("*");
            }
            System.out.println();
            if (i < size/2+1) {
                odd += 2; // columns increasing till center row 
                nos -= 1; // spaces decreasing till center row 
            } else {
                odd -= 2; // columns decreasing
                nos += 1; // spaces increasing

            }
        }
    }
}

如您所见nos,是空格数。直到中间行需要减少,并且需要增加星星的数量,但在中间行之后则相反,即空间增加,星星减少。

size可以是任何数字。我在这里将它设置为 9,所以我将有一个 9 号星形,最大 9 行和 9 列...空间数(nos)将是9/2 = 4.5。但是java会把它当作4,因为int不能存储十进制数,并且中心行会是9/2 + 1 = 5.5,这将导致5 as int

所以首先你会做行......因此有 9 行

(int i=1;i<=size;i++) //size=9

然后像我一样打印空格

(int k =nos; k>=1; k--) //nos being size/2

然后终于星星

(int j=1; j<= odd;j++)

一旦线路结束......

您可以使用 if 条件调整星号和空格。

于 2015-10-29T05:46:33.947 回答
3
for (int i = 0; i < 5; i++)
    System.out.println("    *********".substring(i, 5 + 2 * i));

for (int i = 5; i > 0; i--)
    System.out.println("     **********".substring(i - 1, 5 + (2 * i) - 3));
于 2013-12-28T16:06:15.223 回答
1
public class MyDiamond {
    public static void main(String[] args) {
        //Length of the pyramid that we want.151 is just an example
        int numRows = 151;
        //midrow is the middle row and has numRows number of *
        int midrow = (numRows + 1) / 2;

        int diff = 0;
        for (int i = 1; i < numRows + 1; i++) {
            for (int j = 1; j < numRows + 1; j++) {
                if (((midrow - diff) <= j && (j <= midrow + diff))) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            if (i < midrow) {
                diff++;
            } else {
                diff--;
            }
        }
    }
}
于 2015-04-17T01:11:23.523 回答
1
public class Diamond {
    //Size of the diamond
    private int diagonal;

    public Diamond(int diagonal) {
        this.diagonal = diagonal;
    }

    public void drawDiamond() {
        int n = diagonal;
        for (int i = n / 2; i >= -n / 2; i--) {
            for (int k = 0; k < i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (n - i * 2) && i >= 0; j++) {
                System.out.print("*");
            }
            for (int k = 1; k <= -i && i < 0; k++) {
                System.out.print(" ");
            }
            for (int j = (n / 2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        //You pass diamond size here in the constructor
        Diamond a = new Diamond(21);
        a.drawDiamond();
    }
}

主要问题是对角线的奇偶性。如果它甚至你不能正确绘制顶部星号。所以有 2 种类型的钻石 - 偶数和奇数对角线(顶部有 2 和 1 个星号)。

于 2013-10-11T21:55:47.673 回答
1

在此处输入图像描述

注意:使用 Count 全局变量,我们可以管理空间以及星号的递增和递减。

import java.util.*;
public class ParamidExample {
    public static void main(String args[]) {
        System.out.println("Enter a number");
        Scanner sc = new Scanner(System.in);
        int no = sc.nextInt();

        int count = 1;
        for (int i = 1; i <= 2 * no - 1; i++) {
            for (int j = count; j <= no; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= count * 2 - 1; k++) {
                System.out.print("* ");
            }
            if (i < no)
                count++;
            else
                count--;
            System.out.println("");
        }
    }
}
于 2017-06-09T11:47:03.870 回答
0
import static java.lang.System.out;
import java.util.Scanner;

public class Diamond {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        sc.close();
        Diamond d = new Diamond();
        d.upperDiamond(row);
        d.lowerDiamond(row - 2);
    }

    public void upperDiamond(int a) {
        for (int i = 0; i < a; i++) {
            for (int j = a - 1; j > i; j--)
                out.print(" ");
            for (int k = 0; k < 2 * i - 1; k++)
                out.print("*");
            out.print("\n");
        }
    }

    public void lowerDiamond(int b) {
        for (int i = 0; i < b; i++) {
            for (int j = 0; j <= i; j++)
                out.print(" ");
            for (int k = 0; k < 2 * (b - i) - 1; k++)
                out.print("*");
            out.print("\n");
        }
    }
}
于 2014-03-10T11:27:55.467 回答
0
import java.util.Scanner;

public class Diamond {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        int min = 1;
        for (int i = 0; i < input; i++) {
            for (int j = input - 1; j > i; j--) {
                System.out.print(" ");
            }
            for (int k = 0; k < min; k++) {
                if (k % 2 == 0) {
                    System.out.print("*");
                } else {
                    System.out.print(".");
                }
            }
            min += 2;
            System.out.println();
        }
        int z = input + input - 3;
        for (int i = 1; i < input; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k < z; k++) {
                if (k % 2 == 0) {
                    System.out.print("*");
                } else {
                    System.out.print(".");
                }
            }
            z -= 2;
            System.out.println();
        }
    }
}
于 2014-03-29T10:28:13.847 回答
0

我可以看到您正在尝试做什么,这是思考钻石的一种非常巧妙的方式。

当我变为负数时,你会遇到 j 计数器的一些问题。看看如何使用 Math.abs()

还可以尝试在带有注释的基本步骤中编写一些伪代码,以使模式清晰:

 //print 5 spaces + 1 star
 //print 4 spaces + 2 stars
 //print 3 spaces + 3 stars
 //print 2 spaces+ 4 stars
 .
 . 
 .  
 //print 5 spaces + 1 star

然后,从字面上用变量(j 和 i)替换数字。

你现在有一个模型。这通常是编程中最难的部分……正确地建立模型。仅当您对模型的工作原理有很好的了解时才开始编码。

一旦你替换了变量,你可以尝试将整个事情转换成一个自动循环。

于 2013-10-11T02:23:22.673 回答
0

这应该有效。您可能只需要大多数方法和printDiamond(_)

import java.util.Scanner;
public class StarsTry {
    public static void main(String[] args) {
        int reader;
        Scanner kBoard = new Scanner(System.in);
        do {
            System.out.println("Insert a number of rows: ");
            reader = kBoard.nextInt();
            printDiamond(reader);
        } while (reader != 0);
    }

    public static void printStars(int n) {
        if (n >= 1) {
            System.out.print("*");
            printStars(n - 1);
        }
    }

    public static void printTopTriangle(int rows) {
        int x = 1;
        for (int j = (rows - 1); j >= 0; j--, x += 2) {
            printSpaces(j);
            printStars(x);
            System.out.print("\n");
        }
    }

    public static void printSpaces(int n) {
        if (n >= 1) {
            System.out.print(" ");
            printSpaces(n - 1);
        }
    }

    public static void printBottomTriangle(int rows, int startSpaces) {
        int x = 1 + (2 * (rows - 1));
        for (int j = startSpaces; j <= (rows) && x > 0; j++, x -= 2) {
            printSpaces(j);
            printStars(x);
            System.out.print("\n");
        }
    }

    public static void printBottomTriangle(int rows) {
        int x = 1 + (2 * (rows - 1));
        for (int j = 0; j <= (rows - 1) && x > 0; j++, x -= 2) {
            printSpaces(j);
            printStars(x);
            System.out.print("\n");
        }
    }

    public static void printDiamond(int rows) {
        printTopTriangle((int) rows / 2 + 1);
        printBottomTriangle((int) rows / 2, 1);
    }
}
于 2013-11-11T03:13:57.443 回答
0
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("    "); //Change a bit if diamonds are not in good shape
            }

            if (i < user_input + 1) {
                for (int b = 0; b < 2 * i + 1; b++) {
                    System.out.print("*  "); //Change a bit if diamonds are not in good shape
                }

            } else if (i > user_input) {
                for (int c = 0; c < 2 * x - 1; c++) {
                    System.out.print("*  "); //Change a bit if diamonds are not in good shape
                }
                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();
        }
    }
  }
}

使用 while 或 for 循环制作菱形时。我认为使用“Math.abs”将是最简单的方法。

您可以通过扫描仪输入数字,当输入数字增加时,钻石会变大。

我使用 Eclipse 制作了这个程序。因此,空间会因您的运行环境而异。像另一个 IDE、CMD 或终端。如果钻石形状不好。换个空格就好了。

于 2016-07-08T05:39:56.540 回答
0

您可以按如下方式打印星号菱形(数学运算符)

int m = 4;
int n = 4;
for (int i = -m; i <= m; i++) {
    for (int j = -n; j <= n; j++) {
        int val = Math.abs(i) + Math.abs(j);
        System.out.print(val > Math.max(m, n) ? " " : "∗&quot;);
        if (j < n) {
            System.out.print(" ");
        } else {
            System.out.println();
        }
    }
}

输出:

        ∗        
      ∗ ∗ ∗      
    ∗ ∗ ∗ ∗ ∗    
  ∗ ∗ ∗ ∗ ∗ ∗ ∗  
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
  ∗ ∗ ∗ ∗ ∗ ∗ ∗  
    ∗ ∗ ∗ ∗ ∗    
      ∗ ∗ ∗      
        ∗        
于 2021-01-15T21:34:21.153 回答
0
public class Main {
public static void main(String[] args) {
    int number = 23,l =1,diff = 1,rem = number/2,rep = 0;
    for(int i=1;i<=number;i++){
        if(i < number/2 +1){
            for(int k=rem;k>=1;k--) 
                System.out.print(" ");
            for(int j=1;j<=l;j++) 
                System.out.print("*");
            diff = 2;
            rem -= 1;
        }
        if(i >= number/2 +1){
            for(int k=0;k<rep;k++) 
                System.out.print(" ");
            for(int j=1;j<=l;j++) 
                System.out.print("*");
            diff =3;
            rep += 1;
        }
        System.out.println();
        l = diff == 2 ? (l + 2) : (l - 2);
    }
}

}

//仅适用于奇数...

于 2022-01-12T14:26:42.153 回答
0

java-11

使用String#repeat作为 Java-11 的一部分引入,您可以在单个循环中使用单个语句来完成。

public class Main {
    public static void main(String[] args) {
        final int MID_ROW_NUM = 5;
        for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
            System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
        }
    }
}

输出:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

通过更改空间,您还可以打印菱形的变体:

public class Main {
    public static void main(String[] args) {
        final int MID_ROW_NUM = 5;
        for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
            System.out.println("  ".repeat(Math.abs(i)) + "* ".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
        }
    }
}

输出:

        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 
于 2021-01-16T14:59:59.970 回答
0

尝试这个

public class Main {
    public static void main(String[] args) {
        int n = 50;
        int space = n - 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < space; j++) {
                System.out.print(" ");
            }

            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println("");
            space--;
        }

        space = 0;

        for (int i = n; i > 0; i--) {
            for (int j = 0; j < space; j++) {
                System.out.print(" ");
            }

            for (int j = 0; j < i; j++) {
                System.out.print("* ");
            }

            System.out.println("");
            space++;
        }
    }
}
于 2020-02-17T14:40:25.273 回答
0
public class Main {
private static int l =1;
public static void main(String[] args) {
    int number =9;
    for(int i=1;i<=2*number -1;i++){
        if(i<=number){
            for(int j=1;j<=(number-i);j++)
                System.out.print("   ");
            for(int j=1;j<=i;j++)
                System.out.print("*     ");
        }
        if(i>number){
            for(int j=1;j<=i-number;j++)
                System.out.print("   ");
            for(int j=1;j<=number-l;j++)
                System.out.print("*     ");
            l += 1;
        }
        System.out.println();
    }
}

}

于 2022-01-13T07:21:17.383 回答
0
package com.DiamondPrintingProgram;

import java.util.Scanner;

public class DiamondPrintingProgram {

    public static void main(String[] args) { 
        int num = getInput();
        int middle = (int) num / 2 + 1;
        printOutput(num,middle); 
    }
    public static int getInput() {
        Scanner sc = new Scanner(System.in);
        int num;
        System.out.print("Enter a odd number: ");
        while (true) {
            num = sc.nextInt();
            if (num % 2 != 0) {
                break;
            }
            System.out.print("Please Enter a ODD NUMBER: ");
        }
        return num;
    }
    private static void printOutput(int num, int middle){
        char asterisk = '*';
        for (int j = 0; j < num; j++) {
            for (int i = 1; i <= num; i++) {
                if (j < middle) {
                    if ((i < (middle - j) || i > (middle + j))) {
                        System.out.print(' ');
                    } else {
                        System.out.print(asterisk);
                    }
                } else {
                    if ((i < (j - middle + 2)) || (i > (2 * num - j - middle))) {
                        System.out.print(' ');
                    } else {
                        System.out.print(asterisk);
                    }
                }
            }
            System.out.println();
        }
    }
}
于 2020-06-28T20:51:08.467 回答
0

我在我的大学里有确切的课业,这也需要我在 3 个 for 循环中完成。

我就是这样做的。

为了简单的解释,我将钻石分为两部分。

不。行数 不。空间 不。星星 总数 插槽数
1 4 1 5
2 3 3 6
3 2 5 7
4 1 7 8
5 0 9 9
6 1 7 8
7 2 5 7
8 3 3 6
9 4 1 5

我想找到那个号码。插槽和编号。每行的空格数,然后分配编号。星星真的很容易。

并考虑到没有。插槽中,第 1 - 5 行和第 6 - 9 行将成为两个独立的组(即 middleLine)。

号的等式。前半部分的插槽数将是 numberOfLines(ie i) + (middleLine - 1),其中当 maxNumberOfLines 为 9 时,middleLine - 1 将为 4。

号的等式。后半部分的插槽数将是 middleLine(即替换 I)+(middleLine - 1)(即与上述相同)-(i - middleLine),其中当 i = 6 时,i - middleLine 将为 -1。

对于空间,前半部分将是 middleLine - i,后半部分将是 i - middleLine,它们恰好处于负关系(或关于它们的斜率对称)。

public class printOutDiamondWith3Loops {

public static void main(String[] args) {
    
    int userInput = 9;

    double maxNumberOfLines = userInput;
    // double type is used instead of integer type in order to prevent removal of remainder when a division performed.
    double middleLine = Math.ceil(maxNumberOfLines/2);

    // Print out the diamond.
    for (int i = 1; i <= maxNumberOfLines; i++) { 
    // Determine the number of lines, which is also the maximum number of slots (the line in the middle).
        if (i <= middleLine) { 
        // Separate the whole diamond into two parts(as mentioned above).
            for (int j = 1; j <= i + ((middleLine - 1)); j++) {
            // Determine the no. of slots in each line from line 1 to 5.
                if (j <= middleLine - i) { 
                // Determine the no. of spaces and stars.
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }
        } else { // i > middleLine
            for (int k = 1; k <= (middleLine + (middleLine - 1)) - (i - middleLine); k++) {
            // Determine the no. of slots in each line from line 6 to 9.
            // For better understanding, I did not simplify the above condition.
            // Noticeably, the first middleLine in above for loop is a replacement of i.
                if (k <= i - middleLine) {
                // Determine the no. of spaces and stars.
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }
        } 
        System.out.println();   
    }

}

有了这样的框架,做进一步的改变就容易多了,比如让用户输入号码。他们想要的线条。

希望这个答案可以帮助你。

我可以借给你一个更详细的版本,虽然不一定需要(上面的解释已经解释了这些概念):print-Out-Diamond-With-3-Loops-Advanced-Version.java

于 2020-12-31T08:13:38.533 回答
-1
package practice;

import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            if (i <= 5) {
                for (int k = 1; k <= 5 - i; k++) {
                    System.out.print(" ");
                }
                for (int j = 0; j <= i; j++) {
                    System.out.print(" *");
                }
            }
            if (i > 5) {
                for (int k = 0; k <= i - 6; k++) {
                    System.out.print(" ");
                }
                for (int j = 0; j <= 10 - i; j++) {
                    System.out.print(" *");
                }
            }
            System.out.println();
        }
    }
}
于 2015-06-11T15:52:31.853 回答
-1
class Inc_Dec {
    public static void main(String[] args) {
        int le = 11;
        int c = 0;
        int j1 = (le / 2) + 1;
        int j2 = le - j1;
        for (int i = 1; i <= le; i++) {
            if (c < j1) {
                for (int k = (j1 - i); k > 0; k--) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= i; j++) {
                    System.out.print("*" + " ");
                }
                c++;
                System.out.println();
            } else {
                for (int k = (i - j1); k > 0; k--) {
                    System.out.print(" ");
                }
                for (int j = (le - i + 1); j > 0; j--) {
                    System.out.print("*" + " ");
                }
                System.out.println();
            }
        }
    }
}
于 2016-07-27T10:26:51.157 回答