我想创造一个权力金字塔“*”;使用Java。

我想创造一个权力金字塔“*”;使用Java。,java,Java,输入:基=2,行=3 输出: 输入:基=3,行=3 输出: 我试过这种方法,但我发现空格打印不正确 import java.util.Scanner; public class loops { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("enter base:"); int base = s.n

输入:基=2,行=3
输出:

输入:基=3,行=3
输出:

我试过这种方法,但我发现空格打印不正确

import java.util.Scanner;

public class loops {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("enter base:");
        int base = s.nextInt();

        System.out.println("enter height:");
        int h = s.nextInt(); 


        for (int i = 1; i <= h; i++) {

            int num = (int)Math.pow(base, i);
                for(int n=h-1; n>i-1; n--) {
                        System.out.print(" ");

                  }

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

            System.out.println("");
        }
    }
}
import java.util.Scanner;
公共类循环{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入基:”;
int base=s.nextInt();
System.out.println(“输入高度:”);
int h=s.nextInt();
对于(int i=1;i i-1;n--){
系统输出打印(“”);
}
对于(int j=0;j
导入java.util.Scanner;
公共类循环{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入基:”;
int base=s.nextInt();
System.out.println(“输入高度:”);
int h=s.nextInt();
int spacesNum;
int asterisksNum;

对于(inti=1;i来说,空间的宽度是几何增长的,就像环的宽度一样,但方向相反——它在衰减。也许编码它的最简单方法是考虑总宽度,以及每个环从中得到的东西

考虑到您的代码:

public class loops {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("enter base:");
        int base = s.nextInt();

        System.out.println("enter height:");
        int h = s.nextInt();
        int width = (int) Math.pow(base, h); // Note the total width.

        for (int i = 1; i <= h; i++) {
            int num = (int) Math.pow(base, i);

            // The space is half of what's left after removing the ring.
            for(int j = 0; j < (width - num)/2; j++) {
                 System.out.print(" ");
            }

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

            System.out.println("");
        }
    }
}
公共类循环{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入基:”;
int base=s.nextInt();
System.out.println(“输入高度:”);
int h=s.nextInt();
int width=(int)Math.pow(base,h);//注意总宽度。

对于(int i=1;i您的代码调用
System.out.print
方法指数倍,每个字符调用一次

此外,
System.out.println
在每次迭代中都会被调用,从而导致底层流被刷新(请参阅注释中的链接)

这是一个很好的参考

这不是一个好方法,因为:

  • h
    执行的I/O操作的数量非常昂贵
  • print和println的许多方法调用降低了代码的可读性
  • 使用单独的方法组合字符串,并仅在打印时使用
    System.out.print

    请参考以下代码:

        public static void main(String[] args) {
            //your code here
    
        int totalWidth  = (int) Math.pow(base, h);
        String output = "";
    
        for (int i = 1; i <= h; i++) {
            int numOfStars = (int) Math.pow(base, i);
            int numOfSpace = (int) ((totalWidth - numOfStars) / 2);
            output += composeString(' ', numOfSpace).concat(composeString('*', numOfStars ).concat("\n"));
        }
        System.out.println(output);
        }
    
    
        //Method to create String with same character repeated X number of times
        public static String composeString(char character, int x) {
            StringBuilder buf = new StringBuilder(x);
            while (buf.length() < x) {
                buf.append(character);
            }
            return buf.toString();
        }
    
    publicstaticvoidmain(字符串[]args){
    //你的代码在这里
    int totalWidth=(int)Math.pow(base,h);
    字符串输出=”;
    
    对于(int i=1;i)这似乎有点熟悉…@Kyle-相同的旧代码+一些代码…啊,这就解释了:如果您关心的是性能…编译器而不是缓冲区。请使用StringBuilder(int)构造函数而不是“”.Append而不是insert。特别是-和之间的区别是
    insert
    调用中的System.arrayCopy,这非常昂贵。这是一种改进。您可能希望了解和-流的相关信息,除非调用
    System.out.print
    ,否则不会刷新流,除非其中包含“\n”。在此之前,数据会传入它是缓冲的,与StringBuffer非常相似。这不像预先构建字符串然后一次性发送它那样优化,但它不是在每个System.out.print调用中都执行IO。@MichaelT由于此线程中的所有帖子,包括OP的代码调用
    println
    在每次迭代中导致流刷新,我应该有b更为明确的是,编辑了答案。再次感谢:-)
    import java.util.Scanner;
    
    public class loops {
        public static void main(String[] args) {
            Scanner s=new Scanner(System.in);
            System.out.println("enter base:");
            int base = s.nextInt();
    
            System.out.println("enter height:");
            int h = s.nextInt();
    
            int spacesNum;
            int asterisksNum;
    
            for (int i = 1; i <= h; i++) {
    
                spacesNum = (int) ((Math.pow(base, h) - Math.pow(base, i)) / 2);
                asterisksNum = (int) (Math.pow(base, i));
    
                for (int j = 0; j < spacesNum; j++) {
                    System.out.print(" ");
                }
    
                for (int j = 0; j < asterisksNum; j++) {
                    System.out.print("*");
                }
    
                System.out.println();
    
            }
    
            s.close();
        }
    }
    
    public class loops {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("enter base:");
            int base = s.nextInt();
    
            System.out.println("enter height:");
            int h = s.nextInt();
            int width = (int) Math.pow(base, h); // Note the total width.
    
            for (int i = 1; i <= h; i++) {
                int num = (int) Math.pow(base, i);
    
                // The space is half of what's left after removing the ring.
                for(int j = 0; j < (width - num)/2; j++) {
                     System.out.print(" ");
                }
    
                for (int j = 0; j < num; j++) {
                    System.out.print("*");
                }
    
                System.out.println("");
            }
        }
    }
    
        public static void main(String[] args) {
            //your code here
    
        int totalWidth  = (int) Math.pow(base, h);
        String output = "";
    
        for (int i = 1; i <= h; i++) {
            int numOfStars = (int) Math.pow(base, i);
            int numOfSpace = (int) ((totalWidth - numOfStars) / 2);
            output += composeString(' ', numOfSpace).concat(composeString('*', numOfStars ).concat("\n"));
        }
        System.out.println(output);
        }
    
    
        //Method to create String with same character repeated X number of times
        public static String composeString(char character, int x) {
            StringBuilder buf = new StringBuilder(x);
            while (buf.length() < x) {
                buf.append(character);
            }
            return buf.toString();
        }