将整个代码从while循环转换为Java中的For循环

时间:2017-02-13 13:58:53

标签: java for-loop while-loop

我尝试将代码从while循环转换为For循环,但我没有得到所需的输出。

For循环代码是:

public static void diamond1() {
        System.out.println("Diamond Height: " + DIAMOND_SIZE);
        System.out.println("Output for: For Loop");

        int noOfRows = DIAMOND_SIZE;
        int md=noOfRows%2;


        //Getting midRow of the diamond
        int midRow = (noOfRows)/2;

        //Printing upper half of the diamond
        for (int i = noOfRows; i >= 0;i=(i-2))
        {
            //Printing i spaces at the beginning of each row
            for (int j = 1; j <= i-md; j++) {
                System.out.print(" ");
            }
            //Printing j *'s at the end of each row
            for (int j = 1; j <= (noOfRows+1-md); j++) {
                if (i-md==0 && j==midRow+1) {
                    System.out.print("o ");
                }
                else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }

        //Printing lower half of the diamond
        for (int i = 2; i <= noOfRows;i=(i+2)) {
            //Printing i spaces at the beginning of each row
            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }

            //Printing j *'s at the end of each row
            for (int j=0; j <= (noOfRows); j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }

我得到的输出是:

* * * 
* o * 
  * * * * 

需要的输出是:

  * 
* o * 
  *

我原来的while循环是:

public static void diamond2() {

            System.out.println("");
            System.out.println("Output for: While loop");

    int noOfRows = DIAMOND_SIZE;
    int md=noOfRows%2;

    //Getting midRow of the diamond
    int midRow = (noOfRows)/2;
    int i = noOfRows;
    while(i >= 0){
        //Printing i spaces at the beginning of each row
        int j = 1;
        while(j <= i-md){
            if(i-md==0)break;
            System.out.print(" ");
            j++;
        }

        //Printing j *'s at the end of each row
        while(j <= (noOfRows+1-md)){
            if (i-md==0 && j==midRow+1) {
                System.out.print("o ");
            }
            else {
              System.out.print("* ");  
            }
            j++;
        }
        System.out.println();
        i=(i-2);
    }

    i = 2;
    while(i <= noOfRows){
    //Printing i spaces at the beginning of each row
        int j = 1;
        while(j <= i){
            System.out.print(" ");
            j++;
        }

        //Printing j *'s at the end of each row
        while(j <= (noOfRows+1-md)){

                System.out.print("* ");
                j++;

        }
                 System.out.println();
                 i=(i+2);
        }

    }

有人可以帮我弄清楚我在做错了吗?

3 个答案:

答案 0 :(得分:0)

而不是比较循环,你应该集中在调试时代码出错的地方。这可以帮助您确定问题所在,然后您可以将其与原始代码进行比较。

对我来说,快速查看下方菱形中的最后一个while循环显示您有以下

 //Printing j *'s at the end of each row
        while(j <= (noOfRows+1-md)){

但是你的最后一个for循环有这个;

//Printing j *'s at the end of each row
            for (int j=0; j <= (noOfRows); j++) {

所以在你比较j <= (noOfRows + 1 - md)的同时 但在for循环中,您只是比较j <= noOfRows

这似乎是个问题。

答案 1 :(得分:0)

当你在while循环中使用j时,你将它用作原始while循环的变量。在for循环中,您在嵌套for循环中使用j,这会导致j计数丢失。要修复此添加的全局int变量,并在顶部菱形循环的末尾添加2的值。在底部循环中,它在第一个嵌套循环结束时设置为j + 1的值。以下是for循环的完成代码:

int count = 0;
//Printing upper half of the diamond
for (int i = noOfRows; i >= 0; i-=2) {
    //Printing i spaces at the beginning of each row
    for (int j = 1; j <= i-md; j++) {
        if(i-md==0) {
            break;
        }
        System.out.print(" ");
    }
    //Printing j *'s at the end of each row
    for (int j = noOfRows - count; j <= (noOfRows+1-md); j++) {
        if (i-md==0 && j==midRow+1) {
            System.out.print("o ");
        }
        else {
           System.out.print("* ");
        }
    }
    System.out.println();
    count += 2;
}

//Printing lower half of the diamond
count = 0;
for (int i = 2; i <= noOfRows; i+=2) {
    //Printing i spaces at the beginning of each row
    for (int j = 1; j <= i; j++) {
        System.out.print(" ");
        count = j+1;
    }
    //Printing j *'s at the end of each row
    for (int j = count; j <= (noOfRows); j++) {
        System.out.print("* ");
    }
    System.out.println();
}

答案 2 :(得分:0)

我认为只是将while循环更改为for循环并不是预期的。无论如何,有一种直接的方式来完成你想要的东西。

Java中的for循环有三个部分:

  • 初始值
  • 每次迭代前的条件
  • 每次迭代后的操作

我们使用此功能分三步从while转换为for

  1. 在第一步中,您将获取所有while循环并将其替换为for循环,仅填充条件。例如for(;j <= i - md;)
  2. 在第二步中搜索动作。如果您找到一个,在大多数情况下是while循环的最后一行,请将它们放入相应for循环的操作部分。例如for(;j <= i - md; j++)注意!有些情况下这是不容易的。
  3. 最后一步是管理您的初始状态。确定可以为for循环提供初始状态的情况,因为变量不在循环的范围之后(之后)使用。然后,您可以将最后一个值赋值作为初始状态放入循环中。例如for(int j=1;j <= i - md; j++)注意!在某些情况下,这是不容易实现的。在你的情况下,这一步毫无意义。
  4. 解决方案将如下所示:

    public static void diamond2() {
    
        System.out.println("Solved by Stackoverflow.com. Have fun with it.");
        System.out.println("Output for: For loops");
    
        int noOfRows = DIAMOND_SIZE;
        int md = noOfRows % 2;
    
        // Getting midRow of the diamond
        int midRow = (noOfRows) / 2;
        int i = noOfRows;
        for (; i >= 0; i = (i - 2)) {
            // Printing i spaces at the beginning of each row
            int j = 1;
            for (; j <= i - md; j++) {
                if (i - md == 0)
                    break;
                System.out.print(" ");
            }
    
            // Printing j *'s at the end of each row
            for (; j <= (noOfRows + 1 - md); j++) {
                if (i - md == 0 && j == midRow + 1) {
                    System.out.print("o ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }
    
        for (i = 2; i <= noOfRows; i = (i + 2)) {
            // Printing i spaces at the beginning of each row
            int j = 1;
            for (; j <= i; j++) {
                System.out.print(" ");
            }
            // Printing j *'s at the end of each row
            for (; j <= (noOfRows + 1 - md); j++) {
                System.out.print("* ");
                j++;
            }
            System.out.println();
        }
    
    }
    

    顺便说一下,你的代码不正确。 对于相同的钻石尺寸不起作用。如果你想解决这个问题:抛弃你的代码,坐下来考虑到中间的距离。玩得开心。