Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java沙漏_Java - Fatal编程技术网

Java沙漏

Java沙漏,java,Java,我只差一点点。我想要的是: ******* ***** *** * *** ***** ******* 我得到了什么 ******* ***** *** * * *** ***** ******* 代码 public class HD404 { public static void main(String[] args) { int N = StdIn.readInt(); int x = N*2-1;

我只差一点点。我想要的是:

*******
 *****
  ***
   *
  ***
 *****
*******
我得到了什么

*******
 *****
  ***
   *
   *
  ***
 *****
*******
代码

public class HD404 {
    public static void main(String[] args) {

        int N = StdIn.readInt();
        int x = N*2-1;

        for (int i = 0; i < N; i++) {
            for (int j = i; j > 0; j--) {
                StdOut.print(" ");
            }
            for (int k = 0; k < x; k++) {
                StdOut.print("*");
            }
            x-=2;
            StdOut.println();
        }

        x = 1;
        for (int i = 0; i < N; i++) {
            for (int j = i; j < N-1; j++) {
                StdOut.print(" ");
            }
            for (int k = 0; k < x; k++) {
                StdOut.print("*");
            }
            x += 2;
            StdOut.println();
        }

    }
}
公共类HD404{
公共静态void main(字符串[]args){
int N=StdIn.readInt();
int x=N*2-1;
对于(int i=0;i0;j--){
打印标准件(“”);
}
对于(int k=0;k

现在我只是在猜测,我只是不能指出我的错误。我在这里遗漏了什么?

问题是您开始用1个星号(
x=1
)而不是3来绘制沙漏的底部

第二个问题是沙漏的底部只有
N-2
行,而不是
N-1
,因此循环应该从1开始,而不是从0开始。这是因为带一个星号的线已经在上半部分画好了

更正代码:

public static void main(String[] args) {

    int N = StdIn.readInt();
    int x = N*2-1;

    for (int i = 0; i < N; i++) {
        for (int j = i; j > 0; j--) {
            StdOut.print(" ");
        }
        for (int k = 0; k < x; k++) {
            StdOut.print("*");
        }
        x-=2;
        StdOut.println();
    }

    x = 3; // <-- not 1 here, the first line has 3 asterisks
    for (int i = 1; i < N; i++) { // <-- i starts at 1 because the first line was already drawn in the upper half
        for (int j = i; j < N-1; j++) {
            StdOut.print(" ");
        }
        for (int k = 0; k < x; k++) {
            StdOut.print("*");
        }
        x += 2;
        StdOut.println();
    }

}
样本输出:

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

问题在于代码的第二部分,你要求画一颗星,从零开始,从一开始


解决方案
x=1;
对于(int i=0;i
应替换为

x = 3;
for (int i = 1; i < N; i++)
x=3;
对于(int i=1;i
我能想到的最简单的方法可能是阻止第一个外循环的最后一次迭代,这样就可以阻止显示第一条单星线

我可能会这样做:

for(int i = 0; i < N && x > 1; i++)
{
    /*Code of the first inner loop*/
}
for(int i=0;i1;i++)
{
/*第一个内循环的代码*/
}

适用于那些仍在寻找关于沙漏挑战的更简单、更少代码的人。这只包含2个for循环

您可以将此作为参考

public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;

for(int i=0; i<dimension; i++) {
    int actual = space;
    for (int j=dimension; j > 0; j--) {
        if(actual > 0) {
            System.out.print(" ");
            actual--;
        }
        else {
            System.out.print("*");
            if(stars==printed) {
                actual = space;
                printed = 0;
            } else {
                actual = 1;
                printed++;
            }
        }
    }
    if(i <= size-2) { // will pattern spaces and stars from top to middle
        space++;
        stars--;
    }
    else { // will pattern spaces and stars from middle to top
        space--;
        stars++;
    }
    System.out.println();
}
publicstaticvoid沙漏(int-size){
//2只适用于循环
int维度=(大小*2)-1,空格=0,星星=size-1,打印=0;
对于(int i=0;i 0;j--){
如果(实际值>0){
系统输出打印(“”);
实际--;
}
否则{
系统输出打印(“*”);
如果(星号==打印){
实际=空间;
打印=0;
}否则{
实际值=1;
印刷++;
}
}
}

如果(我有试过调试器吗?只要看看你的代码在第一首单曲*被打印出来投票后会发生什么。这篇文章比99%的类似文章要好,因为他显示了他确实尝试过。如果你想在老问题上添加答案,请解释为什么你的答案比其他答案更好/不同。如果你不这样做,它会变得不一样。)鼓励滚动到底部的人思考您的代码。
for(int i = 0; i < N && x > 1; i++)
{
    /*Code of the first inner loop*/
}
public static void hourGlass(int size) {
// 2 for loops only
int dimension = (size * 2) - 1, space = 0, stars = size - 1, printed = 0;

for(int i=0; i<dimension; i++) {
    int actual = space;
    for (int j=dimension; j > 0; j--) {
        if(actual > 0) {
            System.out.print(" ");
            actual--;
        }
        else {
            System.out.print("*");
            if(stars==printed) {
                actual = space;
                printed = 0;
            } else {
                actual = 1;
                printed++;
            }
        }
    }
    if(i <= size-2) { // will pattern spaces and stars from top to middle
        space++;
        stars--;
    }
    else { // will pattern spaces and stars from middle to top
        space--;
        stars++;
    }
    System.out.println();
}