Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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/algorithm/10.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-loop打印java中的倾斜金字塔模式?_Java_Algorithm_Loops_For Loop - Fatal编程技术网

如何在java中使用for-loop打印java中的倾斜金字塔模式?

如何在java中使用for-loop打印java中的倾斜金字塔模式?,java,algorithm,loops,for-loop,Java,Algorithm,Loops,For Loop,我一直在尝试在java中使用for循环语句打印不同的模式。现在我想学习如何打印以下图案。它基本上是一种倾斜的金字塔型图案 * ** *** **** *** ** * 我试图做到这一点,但我确实得到了正确的结果,但问题是,我认为我这样做的方式是一种不方便的方式。代码如下: for (int x = 1; x <= 4; x++) { for (int y = 1; y <= x; y++) { Syst

我一直在尝试在java中使用for循环语句打印不同的模式。现在我想学习如何打印以下图案。它基本上是一种倾斜的金字塔型图案

    *
    **
    ***
    ****
    ***
    **
    *
我试图做到这一点,但我确实得到了正确的结果,但问题是,我认为我这样做的方式是一种不方便的方式。代码如下:

for (int x = 1; x <= 4; x++) {
    for (int y = 1; y <= x; y++) {
        System.out.print("*");
    }
    System.out.println();
}
for (int x = 1; x <= 3; x++) {
    for (int y = 3; y >= x; y--) {
        System.out.print("*");
    }
    System.out.println();
}

您的代码生成正确的输出,并且对我来说足够清晰。我唯一的建议是使用一个变量来表示金字塔的大小,而不是硬编码的值

您可以以更紧凑的方式编写,只需使用2个循环,如下所示:

int size = 7;
for (int row = 0; row < size; row++) {
    for (int column = 0; column <= Math.min(size-1-row, row); column++) {
        System.out.print("*");
    }
    System.out.println();
}
这里的想法是:

对于棱锥体的每一行,使该行从0变为大小 我们需要确定要打印多少个星号。如果我们在金字塔的上半部分,它等于我们所在的那一排。如果我们在金字塔的下半部分,它等于1行大小随行数的增加而减少。所以星号的计数是行和size-1-row的最小值:这是使用Math.minsize-1-row,row在单个语句中计算出来的。
您的代码生成正确的输出,并且对我来说足够清晰。我唯一的建议是使用一个变量来表示金字塔的大小,而不是硬编码的值

您可以以更紧凑的方式编写,只需使用2个循环,如下所示:

int size = 7;
for (int row = 0; row < size; row++) {
    for (int column = 0; column <= Math.min(size-1-row, row); column++) {
        System.out.print("*");
    }
    System.out.println();
}
这里的想法是:

对于棱锥体的每一行,使该行从0变为大小 我们需要确定要打印多少个星号。如果我们在金字塔的上半部分,它等于我们所在的那一排。如果我们在金字塔的下半部分,它等于1行大小随行数的增加而减少。所以星号的计数是行和size-1-row的最小值:这是使用Math.minsize-1-row,row在单个语句中计算出来的。 这对我很有用:

public class DrawPattern {
    public static void main(String[] args) {
        int i, j;
        int num = 7;
        for (i = 0; i <= num; i++) {
            for (j = 0; j <= num; j++) {
                if (isConditionMatch(num, i, j)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }

    private static boolean isConditionMatch(int num, int i, int j) {
        return (i>j && i+j<=num);
    }
}
这对我很有用:

public class DrawPattern {
    public static void main(String[] args) {
        int i, j;
        int num = 7;
        for (i = 0; i <= num; i++) {
            for (j = 0; j <= num; j++) {
                if (isConditionMatch(num, i, j)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }

    private static boolean isConditionMatch(int num, int i, int j) {
        return (i>j && i+j<=num);
    }
}

基于scanner方法的java模式

1
12
123
1234
12345
123456   
解决方案:

import java.util.*;
public class Pattern{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
int row=s.nextInt();
System.out.println("pattern is here");
for(int i=0;i<=row;i++)
{
for(int j=0; j<=row;j++)
{
System.out.print(j+"");

 }
System.out.println();

}
s.close();

}
}

基于scanner方法的java模式

1
12
123
1234
12345
123456   
解决方案:

import java.util.*;
public class Pattern{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
int row=s.nextInt();
System.out.println("pattern is here");
for(int i=0;i<=row;i++)
{
for(int j=0; j<=row;j++)
{
System.out.print(j+"");

 }
System.out.println();

}
s.close();

}
}

使用以下方法绘制以下形状:

函数调用:

公共静态无效字符串[]args{ int步数=5; 布尔UseSaerator=true; 金字塔台阶,使用计算器; 金字塔_Leftsteps,useSaperator,false; 金字塔_Leftsteps,useSaperator,true; } 公共静态无效打印对象{ System.out.printo; } 函数调用的输出:

1              |    1                  |   1
22             |    2 * 2              |   2 * 2
333            |    3 * 3 * 3          |   3 * 3 * 3
4444           |    4 * 4 * 4 * 4      |   4 * 4 * 4 * 4
55555          |    5 * 5 * 5 * 5 * 5  |   5 * 5 * 5 * 5 * 5
4444           |    4 * 4 * 4 * 4      |
333            |    3 * 3 * 3          |   isTriangle = true
22             |    2 * 2              |
1              |    1                  |
    1          |            1          |
   222         |          2 2 2        |
  33333        |        3 3 3 3 3      |
 4444444       |      4 4 4 4 4 4 4    |
555555555      |    5 5 5 5 5 5 5 5 5  |
               |                       |
useSaperator   |       useSaperator    |
   false       |           true        |
公共静态无效pyramidint步骤,布尔useSaperator{ 字符串saperator=; 对于int i=1;i 0{ 打印机; } } //左 对于int j=i;j>0;j-{ 普林蒂; 如果使用运算符(&j)-1>0{ 打印机; } } //右-完成左后,避开中间元素So,i-1 对于int k=i-1;k>0;k-{ 如果使用操作员{ 打印机; } 普林蒂; } 打印\n; } } 公共静态无效棱锥体\u左阶,布尔useSaperator,布尔isTriangle{ 字符串saperator=*; 对于int i=1;i 0{ 打印机; } } k-; 打印\n; } } } }
使用以下方法绘制以下形状:

函数调用:

公共静态无效字符串[]args{ int步数=5; 布尔UseSaerator=true; 金字塔台阶,使用计算器; 金字塔_Leftsteps,useSaperator,false; 金字塔_Leftsteps,useSaperator,true; } 公共静态无效打印对象{ System.out.printo; } 函数调用的输出:

1              |    1                  |   1
22             |    2 * 2              |   2 * 2
333            |    3 * 3 * 3          |   3 * 3 * 3
4444           |    4 * 4 * 4 * 4      |   4 * 4 * 4 * 4
55555          |    5 * 5 * 5 * 5 * 5  |   5 * 5 * 5 * 5 * 5
4444           |    4 * 4 * 4 * 4      |
333            |    3 * 3 * 3          |   isTriangle = true
22             |    2 * 2              |
1              |    1                  |
    1          |            1          |
   222         |          2 2 2        |
  33333        |        3 3 3 3 3      |
 4444444       |      4 4 4 4 4 4 4    |
555555555      |    5 5 5 5 5 5 5 5 5  |
               |                       |
useSaperator   |       useSaperator    |
   false       |           true        |
公共静态无效pyramidint步骤,布尔useSaperator{ 字符串saperator=; 对于int i=1;i 0{ 打印机; } } //左 对于int j=i;j>0;j-{ 普林蒂; 如果使用运算符(&j)-1>0{ 打印机; } } //右-完成左后,避开中间元素So,i-1 对于int k=i-1;k>0;k-{ 如果使用操作员{ 打印机; } 普林蒂; } 打印\n; } } 公共静态无效棱锥体\u左阶,布尔useSaperator,布尔isTriangle{ 字符串saperator=*; 对于int i=1;i 0{ 打印机; } } k-; 打印\n; } } } } 您可以使用StringBuilder创建完整的输出字符串所有行并在末尾打印。您可以使用StringBuilder创建完整的输出字符串所有行并在末尾打印。