【发布时间】:2015-01-12 16:09:36
【问题描述】:
我使用Blue J作为参考。
我需要做的是在我已经定义了 2 个常量 MIN = 60 和 MAX = 180 之后,我应该将处理时间定义为 60 到 240 之间的随机整数,而不是常量 120。
问题是我不确定该怎么做。
这是应该在其中实现的类TicketCounter。
public class TicketCounter
{
final static int PROCESS = 120;
final static int MAX_CASHIERS = 10;
final static int NUM_CUSTOMERS = 200;
final static int ARR_TIME = 20;
final static int MIN = 60;
final static int MAX = 180;
public static void main ( String[] args)
{
Customer customer;
LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
int[] cashierTime = new int[MAX_CASHIERS];
int totalTime, averageTime, departs;
System.out.printf("%-25.30s %-30.30s%n", "Number of Cashiers", "Average Time (in seconds)");
/** process the simulation for various number of cashiers */
for (int cashiers=0; cashiers < MAX_CASHIERS; cashiers++)
{
/** set each cashiers time to zero initially*/
for (int count=0; count < cashiers; count++)
cashierTime[count] = 0;
/** load customer queue */
for (int count=1; count <= NUM_CUSTOMERS; count++)
customerQueue.enqueue(new Customer(count*ARR_TIME));
totalTime = 0;
/** process all customers in the queue */
while (!(customerQueue.isEmpty()))
{
for (int count=0; count <= cashiers; count++)
{
if (!(customerQueue.isEmpty()))
{
customer = customerQueue.dequeue();
if (customer.getArrivalTime() > cashierTime[count])
departs = customer.getArrivalTime() + PROCESS;
else
departs = cashierTime[count] + PROCESS;
customer.setDepartureTime (departs);
cashierTime[count] = departs;
totalTime += customer.totalTime();
}
}
}
/** output results for this simulation */
averageTime = totalTime / NUM_CUSTOMERS;
System.out.printf("%10s %30s%n", (cashiers+1), averageTime);
}
}
}
提前谢谢你!
【问题讨论】:
-
代码中的随机性在哪里?如果您需要 60 和 240 之间的随机数,那么 nextInt(241)+60 就足够了。
-
@Xline 这将给出 60-300 的范围
-
@ElliottFrisch 哈哈,有一段时间不使用 java了