2017-02-13 165 views
-3

我试图将while循环中的代码转换为For循环,但是Im没有得到所需的输出。将while循环中的整个代码转换为Java中的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); 
     } 

    } 

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

+1

你的调试器将帮助你在这里。 –

+0

@AndyTurner我知道这可能听起来很愚蠢,但我该如何调试呢? – donk2017

+1

你最初的while循环是什么? –

回答

0

当您在while循环中使用j时,您将它用作原始while循环的变量。在for循环中,您正在使用j嵌套for循环,这将导致您的j计数值丢失。要解决这个问题,请在全局int变量中添加2,并在您的顶级钻石循环的末尾添加2。在底部循环中,在第一个嵌套循环结束时将其设置为值j +1。这里是你的最终代码回路应该是什么样子:

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(); 
} 
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

这似乎是这个问题。

+0

我试着改变它,但没有什么区别。 – donk2017

+0

我也试过调试,但无法弄清楚我出错的地方。 – donk2017

0

我觉得只是改变了while循环,为循环不想要的结果。无论如何,有一个完成你想要的东西的简单方法。

一个for循环在Java中有三个部分:

  • 初始值
  • 条件每次迭代

我们用它来从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++)注意!有些情况下这是不容易的。在你的情况下,这一步毫无意义。然后

    该解决方案将是这样的:

    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(); 
        } 
    
    } 
    

    顺便问一下,你的代码是不正确的。 它不适用于相同的钻石尺寸。如果你想解决这个问题:扔掉你的代码,坐下来考虑到中间的距离。玩的开心。