制作菱形与循环问题

时间:2013-08-25 15:27:46

标签: java

我遇到了创建菱形的问题,我的代码在这里:

package random;

public class asd {

    public static void main(String args[]) {
        for (int j = 1; j <= 4; j++) {
            for (int kong = 4 - j; kong >= 1; kong--) {
                System.out.print(" ");
            }
            for (int xing = 1; xing <= 2 * j - 1; xing++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int a = 1; a <= 3; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(" ");
            }
            for (int c = 5; c >= 1; c -= 2) { // <==== here
                System.out.print("*");

            }
            System.out.println();
        }
    }
}

然而,输出是:

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

我认为问题出在我突出显示的代码中,请看看,欢呼

4 个答案:

答案 0 :(得分:1)

java-11

通过使用作为 Java-11 一部分引入的 String#repeat,您可以使用单循环来完成。

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

输出:

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

通过增加一个字符的空间量,您还可以打印形状的变体:

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

输出:

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

答案 1 :(得分:0)

你指出可能有问题的行是正确的。很惊讶你在上半场做得很好:

    for (int c = 5; c >= 2*a - 1; c -= 1) { // <==== here
        System.out.print("*");

答案 2 :(得分:0)

使用Math.abs会让它变得更容易。

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

答案 3 :(得分:0)

您可以使用两个嵌套的 for 循环 和一个 if else 语句来简化代码。

int n = 4;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++)
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print("*");
        else
            System.out.print(" ");
    System.out.println();
}

输出:

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

另见:Output an ASCII diamond shape using loops