刚学for (int x=1,x<=3;x++)
{
    for(int y=1,y<=3;y++)
   {
         System.out.print("*");
   }
   System.out.println("*");
}打出
****
****
****
内循环总多出一位,怎么回事呢?

解决方案 »

  1.   

    你的内层循环用的是System.out.print("*"),y每增加一次,就打印一个*,总共打印3次,但是,当内层循环结束后,你后面有个System.out.println("*");你的目的是换行,所以这个里面的*不需要,应该删掉,不删的话,就会每打印3个后,再打印一个,就换行,才会出现一行4个*的情况。
      

  2.   

    for (int x=1,x<=3;x++)
    {
        for(int y=1,y<=3;y++)
       {
             System.out.print("*");
       }
    System.out.println();
       System.out.println("*");
    }
    运行之后比较一下
      

  3.   

    for (int x=1;x<=3;x++)
    {
        for(int y=1;y<=3;y++)
       {
             System.out.print("*");
       }
    System.out.println();
       System.out.println("*");
    }
    for循环的格式:for(表达式1;条件;表达式2){}
    两个都是分号(;),不是(,)