2011-05-19 56 views
0

我应该给这个输出不完整的输出与while循环

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

等等等等5个itirations 但它只显示第2输出

这里是我的代码

public class itiration { 

    public static void main(String args[]){ 

     int counter1 = 1; 
     int counter2 = 1; 
     int counter3 = 1; 

     while(counter1<=5) 
     { 

       while(counter2<=5) 
       { 
        System.out.print("* "); 
        System.out.print(" "); 
        counter2++; 
       } 

      System.out.println(); 

       while(counter3<=5) 
       { 
        System.out.print(" "); 
        System.out.print("* "); 
        counter3++; 
       } 


      System.out.println(); 

      counter1++; 
     } 

    } 

} 

这不是作业

+0

在输出采样,第四行有六个* S: 试试这个。这是一个错字还是应该是这样的? – 2011-05-19 04:52:17

+0

示例序列中的下一行应该是什么?我没有看到任何明显的模式。 – Asaph 2011-05-19 04:53:02

回答

3

你有没有尝试过这个程序用调试器?

提示:在外循环执行第一次迭代之后,counter2和counter3的值是什么?

+0

另外,如果这不是功课,那么它是什么?只是一个自学练习? – 2011-05-19 04:52:45

+0

OHHH谢谢!我忘记了counter2和counter3。 :|现在我学到了一些新东西。所以它们的值不会重置? counter1 ++循环之后? – user759630 2011-05-19 05:05:39

+0

不,您需要明确重置值。看到这个最好的方法是在调试器中逐步完成。 – 2011-05-19 05:08:57

2

您需要在循环中重置counter2counter3(例如在counter1++之后),否则它们将在循环的第一次运行后保持值5,并且内部循环将不再运行。

1

对于主循环的每次迭代,您都不重置counter2和counter3。在你的问题上

int counter1 = 1; 
    while(counter1<=5) 
    {   
     int counter2 = 1; 
     int counter3 = 1;