循环语句应用实例

a.编写程序,使用循环控制语句计算“1+2+3+。。。。+100"的值。
public class Exercise7_1
{
 public static void main( String[] args )
 {
   int sum = 0;
   
  // 累加计算
   for( int i=1; i<101; ++i )
   {
      sum += i;
   }
   
  System.out.println( "1+2+3+……+100 = " + sum );
 }
}
b.编写程序,判断某一年是否为闰年.

public class Exercise7_3 {  public static void main(String[] args)  {   int year = (int)( Math.random() * 10000 );

  System.out.print( year + "年" );      if( ( year%4==0 && year%100!=0 ) || year%400==0 )   {    System.out.println( "是闰年。" );   }   else   {    System.out.println( "不是闰年。" );   }     }

posted @ 2016-02-24 13:11  边晓艳  阅读(232)  评论(0编辑  收藏  举报