我的本意是要输出8行这样的图形(两行星之间空一行),也就是奇数行*顶格,偶数行*向后退一个格,可结果却是只输出一行*,下面的都是空行
* * * * * * * *
 * * * * * * * * 
import javax.swing.*;
public class Print4
{
public static void main(String[] args) 
{
int i = 1, j = 1;
while(i <= 8)
{

while(j <= 8)
{
if(i % 2 != 0)
{
System.out.print("*");
System.out.print(" ");
}

else 
{
System.out.print(" ");
System.out.print("*");
} j++;
}
System.out.println();

i++;
}
System.exit(0);

}
}

解决方案 »

  1.   

    while(j <= 8)
    前面加上 j=1;将j重新赋值一次
      

  2.   

    因为在第一次循环后j就已大于8了,不会继续循环了
    这样就行
    public static void main(String[] args) {
            int i = 1;
            while (i <= 8) {
                int j=1;
                while (j <= 8) {
                    if (i % 2 != 0) {
                        System.out.print("*");
                        System.out.print(" ");
                    } else {
                        System.out.print(" ");
                        System.out.print("*");
                    }
                    j++;            }
                System.out.println();
                i++;
            }
            System.exit(0);
        }
      

  3.   

    class Print
    {
        public static void main(String [] args)
        {
         int x;
        for(int i=0;i<8;i++)
        {
         x=i%2;
         if(x==0){
         int j=0;
           while(j<8){
            System.out.print("* ");
            j++;
            }
           
           System.out.println();
         }
         else{
         int y=0;
         System.out.print(" ");
         while(y<8){
            System.out.print("* ");
            y++;
            }
         System.out.println();
         }
         }
        }
    }