gpt4 book ai didi

java - 求平均值/最小值/最大值

转载 作者:行者123 更新时间:2023-11-30 02:10:23 24 4
gpt4 key购买 nike

   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();
}
}

我的程序的不同部分有这些代码,其中时间记录在变量“duration”中。我在同一类的不同方法中使用了“duration”变量。我想知道是否有一种方法可以添加这些值“时间”并找到平均值/最小值/最大值

最佳答案

您可以定义一个数组来存储每次更改变量之前的时间,然后使用它来计算所需的值。

List<Long> timeHistory = new ArrayList<>();
...
timeHistory.add(duration);
duration = (long)(Math.random()* 3);

timeHistory 应该是你的类的全局变量

关于java - 求平均值/最小值/最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com