黑马程序员_java_for语句实例

来源:互联网 发布:smash it up bgm 编辑:程序博客网 时间:2024/06/02 05:59

---------------------- android培训java培训、期待与您交流! ----------------------

 for语句:

 累加思想,从1加到100:

class sum

{

     public static void main(Stinrg[] args)

         {

                 int sum=0;

                 for(int x=1;x<=100;x++)

                  {

                       sum=x+sum;

                  }

                 System.out.println(sum);

        }

}

从1~100找到,7的倍数的个数,并分别打印

这需要用到计数器

class ForDemo

{

          public static void main(String[] args)

              {

                       int count=0;

                       for(int x=1;x<=100;x++)

                       {

                              if(x%7==0) 

                                  count++;

                        }

                          System.out.println(count);

              }

}

 

嵌套for循环:

 九九乘法表
 1*1=1
 1*2=2 2*2=4

class ForForDemo

{

      public static void main(String[] args)

     {

            for(int x=1;x<=9;x++)

             {

                  for(int y=1;y<=x;y++)

                 {

                    System.out,println(Y+"*"+x=x*y+"\t")

                }

                 System.out.println();

             }         

     }

}

 

/*

打印下面图形

*

**

***

****

*****

*/

class ForForeDemo1

{

         public static void main(String[] args)

          {

                for(int x=1;x<=5;x++)

                 {

                     for(int y=1;y<=x;y++)

                    {

                           System.out.println("*");

                    }

                   System.out.println();

                 }

            }

}

 

 

 

 

原创粉丝点击