分段函数就是根据不同的变量范围,选择执行相应的函数,根据这点,可以用if(变量范围)-else
if(变量范围)-else判断输入的变量值来执行相应的函数。
import java.util.*;
public class RunningTime
{
public static void main(String argc[]) throws InterruptedException
{
int i=0,j=0;
long k=0,begin,end,time;
Date mydate=new Date();
begin=mydate.getTime();
for (;i<100;i++)
{
for(;j<100;j++)
{
Thread.currentThread().sleep(5);
k+=i*j;
}
}
Date mydate2=new Date();
end=mydate2.getTime();
time=end-begin;
System.out.println(begin+"运行时间为:"+time+" "+end);
}
}
计算运算时间的程序