循环语句应用实例
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( "不是闰年。" ); } }
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( "不是闰年。" ); } }
相关文章推荐
- 苹果支付(3)
- PHP编写一个限制实例化次数的类
- 22、微信 - 主界面实现
- Block生命周期
- java.io.StreamCorruptedException: invalid stream header报错原因
- 21、微信 - 主界面布局
- MySQL 清除表空间碎片
- windows8运行php Composer出现SSL报错的问题
- 算法导论—最长公共子序列
- 修该hosts访问谷歌
- 轻松解决listView数据混乱
- php正则验证手机号码
- 如何用手机拍好一个女生
- 了解Linux的基础知识和一般概念
- iOS 拦截系统自带的Pop事件
- leetcode笔记--Length of Last Word
- Laravel输出JSON时设定输出字段的几种情况总结
- Git安装和使用
- 字符数组,字符串指针
- .NET中SQL Server数据库连接方法