if (cleaningbay >=5)
{
long duration =5;
try
{
System.out.println("\n\t All cleaners are busy..Please wait!..................................................................(-_-)" + bus.getName() );
System.out.println("\n\t" + bus.getName() + "waited for " + duration );
TimeUnit.SECONDS.sleep(duration);
bus.notify();
Cleaners(bus);
bus.wait();
}
catch(InterruptedException iex)
{
iex.printStackTrace();
}
}
else if (cleaningbay < 5)
{
System.out.println("\n\t" + bus.getName() + "is heading to " + bus.getCleanersName() + Clock.get_time());
cleaningbay++;
long duration=0;
try
{
System.out.println("\n\t Cleaning in progress :" + bus.getName() + Clock.get_time() );
duration = (long)(Math.random()* 3);
TimeUnit.SECONDS.sleep(duration);
}
catch(InterruptedException iex)
{
iex.printStackTrace();
}
}
我在程序的不同部分有这些代码,其中时间记录在变量&#34; duration&#34;中。我用了#34;持续时间&#34;变量在同一个类的不同方法中。我想知道是否有一种方法可以添加这些值&#34; time&#34;并找到平均/最小/最大
答案 0 :(得分:1)
您可以在每次更改变量之前定义一个数组来存储您的时间,然后使用它来计算您所需的值。
List<Long> timeHistory = new ArrayList<>();
...
timeHistory.add(duration);
duration = (long)(Math.random()* 3);
timeHistory
应该是您班级的全局变量