public class new3
{
 public static void main(String args[])
{
     int i=1;
     int j=1;
    
     for(j=1;j<=5;j=j+1)
       {for(i=1;i<=5;i=i+1)
       if(i!=3)
       System.out.print(" ");
       else
       System.out.print("*");}
     System.out.println("");
}
}
这段代码我的本意是要实现5个竖着排列的*
但是最后运行后却是横着排列的5个*
我很奇怪最后一行应该是在循环之内的,为什么却只在选循环结束后运行了一次?
谢谢!

解决方案 »

  1.   

    public class new3 {
        public static void main(String args[]) {
            int i = 1;
            int j = 1;        for (j = 1; j <= 5; j++) {
                for (i = 1; i <= 5; i++) {//這裡加上一個擴號
                    if (i != 3)
                        System.out.print(" ");
                    else
                        System.out.print("*");
                }
                System.out.println("");
            }
        }//這裡也加上一個 配對
    }