Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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 反转一个while循环形状?_Java_While Loop_Do While - Fatal编程技术网

Java 反转一个while循环形状?

Java 反转一个while循环形状?,java,while-loop,do-while,Java,While Loop,Do While,因此,我试图反转当前生成的形状的输出。我想知道我应该改变这种状况吗?我试图改变变量“a”和“c”的值,结果得到了一个无限循环 class IRT { public static void main(String[] args) { int a = 2; int b; int c = 6; do { b = a - 1; if (b != 0) {

因此,我试图反转当前生成的形状的输出。我想知道我应该改变这种状况吗?我试图改变变量“a”和“c”的值,结果得到了一个无限循环

class IRT {

    public static void main(String[] args) {

        int a = 2;
        int b;
        int c = 6;

        do {
            b = a - 1;
            if (b != 0) {
                do {
                    System.out.print("*");
                    b--;

                } while (b >= 1);
            }
            b = 1;
            do {
                System.out.print(" ");
                b++;

            } while (b <= (c - a + 1));

            System.out.println();
            a++;
        } while (a <= c);

    }

}
   My output                    What i'm trying to get

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

如果您只想颠倒每行的顺序(即先打印空格,然后打印
*
s),只需将顺序更改为两个内部while循环即可

   My output                    What i'm trying to get

   *                                           *
   **                                         **
   ***                                       ***
   ****                                     ****
   *****                                   *****
  int a=2;
  int b;
  int c=6;

  do{
    b=1;
    do{
      System.out.print(" ");
      b++;

    }while(b <= (c - a + 1));

    b = a - 1;
    if (b!=0){
      do{
        System.out.print("*");
        b--;

      }while(b >= 1);
    }

    System.out.println();
    a++;

  } while(a <= c);

正如其他人已经指出的,您应该索引代码并给出有意义的名称。由于更容易完全重写代码,我花了一些时间简化代码并与大家分享以下步骤:

   My output                    What i'm trying to get

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

1:
我将do while循环替换为带有中断的无限循环,因为它们很难读取。我认为<代码>(真) >用<代码>破解< /代码>和<代码>继续<代码>语句(<代码>破解标签< /代码>)作为低级操作,它们的行为类似有意义的代码> Goto S.

   My output                    What i'm trying to get

   *                                           *
   **                                         **
   ***                                       ***
   ****                                     ****
   *****                                   *****
int a = 2;
int b;
int c = 6;

while(true) {
    b = a - 1;
    if (b != 0) {
        while(true) {
            System.out.print("*");
            b--;
            if(b < 1) {
                break;
            }
        }
    }
    b = 1;
    while (true){
        System.out.print(" ");
        b++;
        if(b > (c - a + 1)) {
            break;
        }

    }

    System.out.println();
    a++;
    if(a > c) {
        break;
    }
}

3:
我将临时使用的变量
b
放在for循环init语句中,以阐明变量的生存期及其实际用法
c
从不改变,所以我认为它是一个常量参数

   My output                    What i'm trying to get

   *                                           *
   **                                         **
   ***                                       ***
   ****                                     ****
   *****                                   *****
final int c = 6;

for (int a = 2; a <= c; a++) {
    if (a != 1) {
        for (int b = a - 1; b >= 1; b--) {
            System.out.print("*");
        }
    }
    for (int b = 1; b <= c - a; b++) {
        System.out.print(" ");
    }
    System.out.println();
}

5:
您可能已经注意到,我将
final int c
更改为等于
6
,而不是
5
。这是因为我发现它与行数和宽度有关,但只比它大一个单位。在这一步中,我给变量指定了有意义的名称,除了
I
,因为这是一个临时计数器,用于写入n次字符

   My output                    What i'm trying to get

   *                                           *
   **                                         **
   ***                                       ***
   ****                                     ****
   *****                                   *****
final int size = 5;
for (int row = 0; row < size; row++) {
    for (int i = 0; i < size - (row + 1); i++) {
        System.out.print(" ");
    }
    for (int i = 0; i < row + 2; i++) {
        System.out.print("*");
    }
    System.out.println();
}

我同意埃兰关于缩进的看法。还请使用有意义的变量名,如
行数
——您不会后悔的。