1.使用for循环;
2.使用while循环;
3.使用do-while循环;
4.提早退出循环(break,continue);
5.为循环命名。
程序Nines:显示1-200的整数与9的乘积
1 package com.jsample;
2
3 public class Nines {
4 public static void main(String[] args){
5 for(int dex = 1;dex <= 200; dex++){
6 int multiple = 9 * dex;
7 System.out.println(multiple + " ");
8 }
9 System.out.println();
10 }
11 }
View Code
输出:
9
……
1800
程序Benchmark:查看计算机一分钟之内计算平方根的次数
1 package com.jsample;
2
3 public class Benchmark {//声明Benchmark类
4 public static void main(String[] args){//开始程序的main()块
5 long startTime = System.currentTimeMillis();//创建变量startTime,并用当前时间(毫秒值)为其赋初值
6 long endTime = startTime + 60000;//创建endTime变量,与startTime正好相隔一分钟
7 long index = 0;//创建循环变量index
8 while (true){//无限循环,只能用break破除
9 double x = Math.sqrt(index);//计算index的平方根,存到x变量中
10 long now = System.currentTimeMillis();//创建变量now,并将当前时间赋值给它
11 if (now > endTime){//超过一分钟,跳出循环
12 break;
13 }
14 index++;//循环变量自增
15 }
16 System.out.println(index + " loops in one minute.");//显示其进行平方根计算的次数
17 }
18 }
View Code
输出:
12880432183 loops in one minute.
程序Mod13:找出前400个能被13整除的数
1 package com.jsample;
2
3 public class Mod13 {
4 public static void main(String[] args){
5 int index = 0;
6 for(int flag = 1; flag <= 400; index++){
7 if(index % 13 == 0) {
8 flag++;
9 System.out.println(index + " ");
10 }
11 }
12 }
13 }
View Code
输出:
0
……
5187
希望与广大网友互动??
点此进行留言吧!