您的位置:首页 > 其它

循环语句应用实例

2016-02-24 13:11 453 查看
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( "不是闰年。" ); } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: