Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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 如何镜像不超过四个for循环的沙漏?_Java - Fatal编程技术网

Java 如何镜像不超过四个for循环的沙漏?

Java 如何镜像不超过四个for循环的沙漏?,java,Java,我想知道如何镜像一个半沙漏。。。不超过4个循环。我不想使用递归或数组,只是简单的循环 Scanner sc = new Scanner(System.in); System.out.println(" Enter odd number above 0 here: "); int h = sc.nextInt(); char x = '#'; if (h % 2 != 0) { for (int i = 1; i <= h; i++)

我想知道如何镜像一个半沙漏。。。不超过4个循环。我不想使用递归或数组,只是简单的循环

    Scanner sc = new Scanner(System.in);
    System.out.println(" Enter odd number above 0 here: ");
    int h = sc.nextInt();
    char x = '#';

    if (h % 2 != 0) {
        for (int i = 1; i <= h; i++) {

            // loop for first part of hourglass
            for (int j = 1; j <= i; j++) {
                System.out.print(x);
            }
            // create white space
            for (int j = h - i; j >= 1; j--) {
                System.out.print("  ");
            }
            // create mirror
            for (int k = i; k >= 1; k--) {
                System.out.print(x);
            }

            System.out.println();
        }

    } else {
        System.out.println(" Not an odd number. Try again: ");
    }
Scanner sc=新扫描仪(System.in);
System.out.println(“在此处输入0以上的奇数:”);
int h=sc.nextInt();
字符x='#';
如果(h%2!=0){
对于(int i=1;i=1;k--){
系统输出打印(x);
}
System.out.println();
}
}否则{
System.out.println(“不是奇数,请重试:”;
}

只需对迭代器进行简单更改,即可对循环执行相同的

    Scanner sc = new Scanner(System.in);
    System.out.println(" Enter odd number above 0 here: ");
    int h = sc.nextInt();
    char x = '#';

    if (h % 2 != 0) {
        for (int i = 1; i < 2*h; i++) {

            int a = Math.abs(h-i);
            // loop for first part of hourglass
            for (int j = a; j < h; j++) {
                System.out.print(x);
            }
            // create white space
            for (int j = 0; j <a; j++) {
                System.out.print("  ");
            }
            for (int j = a; j < h; j++) {
                System.out.print(x);
            }

            System.out.println();
        }
    } else {
        System.out.println(" Not an odd number. Try again: ");
    }
Scanner sc=新扫描仪(System.in);
System.out.println(“在此处输入0以上的奇数:”);
int h=sc.nextInt();
字符x='#';
如果(h%2!=0){
对于(int i=1;i<2*h;i++){
int a=数学绝对值(h-i);
//沙漏第一部分的循环
对于(int j=a;jfor(int j=0;j在字符串[]数组中构建前半部分,然后正常打印,然后向后打印。对于垂直对称,也可以这样做。

您可以在一个for循环中完成此操作,并删除所有嵌套的for循环,实际上,请尝试以下操作:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(" Enter odd number above 0 here: ");
    int h = sc.nextInt();
    String x = "#";

    if (h % 2 != 0) {
        for (int i = 1; i <= h; i++) {
            String hash = new String(new char[i]).replace("\0", x);
            System.out.print(hash);
            String spaces = new String(new char[2*(h-i)]).replace("\0", " ");
            System.out.print(spaces);
            System.out.print(hash);

            System.out.println();
        }

    } else {
        System.out.println(" Not an odd number. Try again: ");
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“在此处输入0以上的奇数:”);
int h=sc.nextInt();
字符串x=“#””;
如果(h%2!=0){

for(int i=1;我是真的,谢谢:-)当然可以,但是你认为这是解决这个问题所能使用的for循环的最小数量吗?我的教授告诉我,它可能在四个循环内,所以我尝试:-)@grinsekatz007我编辑它,只有四个循环,它更清楚吗?是的,谢谢,我想我在寻找它…但我还不明白,a是如何以那种方式操纵循环的…这很有趣。谢谢!所以,a主要控制模式,对吗?从中间开始(用h-I完成)对吗?但为什么它现在会向上和向下运行?因为a先减小然后再增大,所以我在正轨上吗?@grinsekatz007注意到a不是h-i而是abs(h-i),即h和i之间的距离可以改进语法。句子以大写字母开头,与德语中相同。