简体   繁体   English

如何打印由用户输入确定的可缩放金字塔尺寸?

[英]How can I print a scalable pyramid in size determined with user input?

I'm taking a beginners Java course over the summer. 我整个夏天都在上初学者Java课程。 I need to make a pyramid using loops for homework. 我需要使用作业循环制作金字塔。 The pyramid has to be made out of asterisks; 金字塔必须用星号制成; in addition, size of pyramid is determined by user. 另外,金字塔的大小由用户确定。

This is what I have for code now; 这就是我现在要编写的代码;

public class Pyramid {

  public static void main(String[] args) {

    int size = 6;
    for (int x = 0; x < size; x++) {
        for (int y = x; y < size; y++) {

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

        }

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

The problem of my code is that the number of asterisks in each row is wrong by one. 我的代码的问题是,每行中的星号数量错了一个。

for (int z = 0; z <= x; z++) {

will execute the loop until z <= x is no longer true. 将执行循环,直到z <= x不再为真。 That means it executes for z=0 , z=1 , z=2 , ..., z=x --which means it actually executes the loop x+1 times. 这意味着它将针对z=0z=1z=2 ,..., z=x执行-这意味着它实际上执行了x+1次循环。 (The next z , z=x+1 , is the first z that makes z<=x false.) (下一个zz=x+1 ,是使z<=x false的第一个z 。)

The normal idiom in Java (and other language with C-like for statements) is to start at 0 and use < when checking for the upper bound: Java(以及其他类似C语言的for语句的语言)中的常规用法是从0开始,并在检查上限时使用<

for (int z = 0; z < x; z++) {

You'll run into cases where you want to use <= , and you'll run into cases where you want to start at 1 instead of 0, but the majority of for loops with an integer index follow this form. 您将遇到要使用<= ,并且会遇到要从1而不是0开始的情况,但是大多数带有整数索引的for循环都遵循这种形式。

If I understand your question correctly : 如果我正确理解您的问题:

 public class Pyramid  {

    public static void main(String[] args) {
       int size =6;
       for (int i = 1; i <= size; i++) {
           for (int x = size - 1; x >= i; x--) {
               System.out.print(" ");
           }

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

           for (int z= 1; z <= i - 1; z++) {
               System.out.print("*");
           }

           System.out.println();
       }
     }
   }

The output is : 输出为:

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

If I understand your question, you could do something like this 如果我理解您的问题,您可以执行以下操作

int levels = 0;
Scanner input = new Scanner(System.in);
while (levels < 1) {
    System.out.println("What size triangle would you like?");
    if (input.hasNextInt()) {
        levels = input.nextInt();
    } else if (input.hasNext()) {
        System.out.println("Not a valid size: " + input.next());
    } else {
        System.err.println("no more input");
        System.exit(1);
    }
}
for (int i = 1; i <= levels; i++) {
    StringBuilder sb = new StringBuilder();
    int t = i;
    while (--t > 0) {
        sb.append("*");
    }
    StringBuilder spaces = new StringBuilder();
    for (t = 0; t < levels - i; t++) {
        spaces.append(' ');
    }
    System.out.println(spaces.toString() + sb + "*" + sb);
}

To solve this problem it's best to think about the numbers that go into it... 为了解决这个问题,最好考虑一下其中的数字...

  *
 ***
*****

If you label the parts of the triangle 如果标记三角形的部分

  *     row 1, 2 spaces, 1 star
 ***    row 2, 1 space, 3 stars
*****   row 3, 0 spaces, 5 starts

Then you can just start playing with the numbers The number of spaces to display is 3 - row # + 1 The number of stars to display is 2 * row - 1 然后,您就可以开始玩数字了。要显示的空格数是3-行号+ 1要显示的星星数是2 *行数-1

Then construct a loop to draw each line. 然后构造一个循环以绘制每条线。 within this loop, you need a loop to draw the number of spaces and a loop to draw the number of stars 在此循环中,您需要一个循环来绘制空格数,并需要一个循环来绘制星星数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM