为什么每次我 运行 我的程序时它都会添加到最后一个
Why does it add on to the last one every time I run my program
基本上,问题是为在银行排队等候的人创建一个模拟。需要某些标准,例如平均等待时间、平均分钟数、平均最长等待时间等。它还要求 运行 我的程序 3 次,每次 1000 次(使用 for 循环)。问题是每次我 运行 代码时,答案都会因为某种原因加起来,我似乎不知道为什么。比如最后的运行本应该模拟运行的次数是1000次,但是却把前面的运行都加起来,给了3000次。
此外,while 循环外的所有变量(averageWaitingTime、averageSimulationTime...等)都给我错误的答案。这是因为,例如,对于平均等待时间,它不断添加相同的等待时间值。我不知道为什么。
public class QueueSimulation {
public static void main(String[] args) {
Queue<Customer> q = new LinkedList<Customer>();
int numServed = 0;
int averageNumServed = 0;
int waitingTime = 0;
int maxWaitingTime = 0;
int averageLongestWaitingTime = 0;
int totalWaitingTime = 0;
double averageWaitingTime = 0;
int maxQueueSize = 0;
int timeDone = 0;
int totalSimulationTime = 0;
int averageSimulationTime = 0;
int numSimulationRan = 0;
Random randomGenerator = new Random();
int time = 0;
int processingTime = 0;
int totalProcessingTime = 0;
for (int i = 0; i < 1000; i++) {
double arrivalRate = 0.2;
int maxProcessingTime = 5;
while (time < 8 * 60) {
if (Math.random() < 0.2) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
for (int j = 0; j < 1000; j++) {
double arrivalRate = 0.5;
int maxProcessingTime = 3;
while (time < 8 * 60) {
if (Math.random() < 0.5) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
for (int k = 0; k < 1000; k++) {
double arrivalRate = 0.68;
int maxProcessingTime = 4;
while (time < 8 * 60) {
if (Math.random() < 0.68) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
}
}
输出:
A new customer arrives at time 5
A new customer arrives at time 10
A new customer arrives at time 11
A new customer arrives at time 12
A new customer arrives at time 25
A new customer arrives at time 28
A new customer arrives at time 31
A new customer arrives at time 45
A new customer arrives at time 48
A new customer arrives at time 51
A new customer arrives at time 59
A new customer arrives at time 64
A new customer arrives at time 68
A new customer arrives at time 86
A new customer arrives at time 100
A new customer arrives at time 106
A new customer arrives at time 108
A new customer arrives at time 113
A new customer arrives at time 115
A new customer arrives at time 120
A new customer arrives at time 124
A new customer arrives at time 125
A new customer arrives at time 126
A new customer arrives at time 132
A new customer arrives at time 137
A new customer arrives at time 153
A new customer arrives at time 156
A new customer arrives at time 164
A new customer arrives at time 201
A new customer arrives at time 206
A new customer arrives at time 208
A new customer arrives at time 219
A new customer arrives at time 226
A new customer arrives at time 233
A new customer arrives at time 234
A new customer arrives at time 237
A new customer arrives at time 242
A new customer arrives at time 246
A new customer arrives at time 247
A new customer arrives at time 251
A new customer arrives at time 262
A new customer arrives at time 271
A new customer arrives at time 276
A new customer arrives at time 277
A new customer arrives at time 282
A new customer arrives at time 285
A new customer arrives at time 287
A new customer arrives at time 292
A new customer arrives at time 296
A new customer arrives at time 298
A new customer arrives at time 318
A new customer arrives at time 319
A new customer arrives at time 327
A new customer arrives at time 336
A new customer arrives at time 337
A new customer arrives at time 338
A new customer arrives at time 346
A new customer arrives at time 355
A new customer arrives at time 356
A new customer arrives at time 358
A new customer arrives at time 362
A new customer arrives at time 363
A new customer arrives at time 366
A new customer arrives at time 374
A new customer arrives at time 379
A new customer arrives at time 380
A new customer arrives at time 384
A new customer arrives at time 389
A new customer arrives at time 400
A new customer arrives at time 407
A new customer arrives at time 416
A new customer arrives at time 418
A new customer arrives at time 424
A new customer arrives at time 427
A new customer arrives at time 433
A new customer arrives at time 436
A new customer arrives at time 437
A new customer arrives at time 438
A new customer arrives at time 446
A new customer arrives at time 454
A new customer arrives at time 466
A new customer arrives at time 469
A new customer arrives at time 471
Average waiting time per customer: 1.0
Average longest waiting time: 2.714
Average number of minutes: 475.0
Longest waiting time: 8
Average number of customers 83.0
Number of times the simulation ran: 1000
Average waiting time per customer: 2.0
Average longest waiting time: 2.714
Average number of minutes: 950.0
Longest waiting time: 8
Average number of customers 166.0
Number of times the simulation ran: 2000
Average waiting time per customer: 3.0
Average longest waiting time: 2.714
Average number of minutes: 1425.0
Longest waiting time: 8
Average number of customers 249.0
Number of times the simulation ran: 3000
有个原则叫Don't Repeat Yourself or DRY。在这个词被创造出来之前,我们曾经开玩笑说懒惰是程序员的一种好品质:如果你必须做某事两次,那么你就经常做一次。
也许您可以考虑声明一些 methods,因为您的程序似乎有 3 次相同的代码。
讽刺的是,有一件事你只能做一次:初始化统计变量:
int numServed = 0;
...
如果您复制此部分并删除类型(因此显示为 numServed = 0;
等),然后再复制一次,它会重置这些变量。
但是,复制粘贴代码是一种不好的做法:现在您需要维护的代码是原来的 3 倍。
要解决此问题,您可以像这样构建程序:
class QueueSimulation
{
public static void main(String[] args) {
for ( int test = 0; test < 3; test ++ ) {
int numServed = 0;
..
for ( int i = 0; i < 1000; i++) {
...
}
System.out.println();
....
}
}
}
没有重复的代码,它对变量进行了本地化,因此它们在每次模拟时都被重置 - 这解决了您的问题。
这只是朝着正确方向迈出的一步。还有一个步骤是 factor out methods at abstraction levels, such as Top-Down and Bottom-Up design.
TLDR:一个好的经验法则是让任何方法适合一页文本。
在你的情况下:你是运行同样的模拟3次。该模拟的细节是什么与方法 运行 模拟 3 次无关。
这是另一个可能的大纲,我们将变量转换为 QueueSimulation
class 的字段。要初始化它们,我们只需实例化一个新的 QueueSimulation。这里是:
class QueueSimulation
{
public static void main(String[] args) {
for ( int test = 0; test < 3; test ++ ) {
QueueSimulation sim = new QueueSimulation();
sim.simulate();
sim.report();
}
}
Queue<Customer> q = new LinkedList<Customer>();
int numServed = 0;
...
int totalProcessingTime = 0;
public void simulate() {
for ( int i = 0; i < 1000 ) {
....
}
}
public void report() {
System.out.println( ....
...
}
}
基本上,问题是为在银行排队等候的人创建一个模拟。需要某些标准,例如平均等待时间、平均分钟数、平均最长等待时间等。它还要求 运行 我的程序 3 次,每次 1000 次(使用 for 循环)。问题是每次我 运行 代码时,答案都会因为某种原因加起来,我似乎不知道为什么。比如最后的运行本应该模拟运行的次数是1000次,但是却把前面的运行都加起来,给了3000次。
此外,while 循环外的所有变量(averageWaitingTime、averageSimulationTime...等)都给我错误的答案。这是因为,例如,对于平均等待时间,它不断添加相同的等待时间值。我不知道为什么。
public class QueueSimulation {
public static void main(String[] args) {
Queue<Customer> q = new LinkedList<Customer>();
int numServed = 0;
int averageNumServed = 0;
int waitingTime = 0;
int maxWaitingTime = 0;
int averageLongestWaitingTime = 0;
int totalWaitingTime = 0;
double averageWaitingTime = 0;
int maxQueueSize = 0;
int timeDone = 0;
int totalSimulationTime = 0;
int averageSimulationTime = 0;
int numSimulationRan = 0;
Random randomGenerator = new Random();
int time = 0;
int processingTime = 0;
int totalProcessingTime = 0;
for (int i = 0; i < 1000; i++) {
double arrivalRate = 0.2;
int maxProcessingTime = 5;
while (time < 8 * 60) {
if (Math.random() < 0.2) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
for (int j = 0; j < 1000; j++) {
double arrivalRate = 0.5;
int maxProcessingTime = 3;
while (time < 8 * 60) {
if (Math.random() < 0.5) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
for (int k = 0; k < 1000; k++) {
double arrivalRate = 0.68;
int maxProcessingTime = 4;
while (time < 8 * 60) {
if (Math.random() < 0.68) {
System.out.println("A new customer arrives at time " + time);
processingTime = randomGenerator.nextInt(maxProcessingTime);
totalProcessingTime += processingTime;
Customer c = new Customer(time, processingTime);
q.add(c);
if (q.size() > maxQueueSize)
maxQueueSize = q.size();
}
if (waitingTime > maxWaitingTime)
maxWaitingTime = waitingTime;
averageLongestWaitingTime += maxWaitingTime;
// serve the next customer
if (time > timeDone) // service is available
if (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
time++;
if (time > 8 * 60)
while (!q.isEmpty()) {
Customer c = q.remove();
timeDone = time + c.getProcessingTime();
// calculate the waiting time of this customer
waitingTime = time - c.getArrivalTime();
// update total waiting time of all customers
totalWaitingTime += waitingTime;
// service this customer
numServed++;
}
}
averageWaitingTime += (totalWaitingTime / numServed);
totalSimulationTime = timeDone;
averageSimulationTime += totalSimulationTime;
averageNumServed += numServed;
numSimulationRan += 1;
}
System.out.println();
System.out.println("Average waiting time per customer: " + (double) averageWaitingTime / 1000);
System.out.println("Average longest waiting time: " + (double) averageLongestWaitingTime / 1000);
System.out.println("Average number of minutes: " + (double) averageSimulationTime / 1000);
System.out.println("Longest waiting time: " + maxWaitingTime);
System.out.println("Average number of customers " + (double) averageNumServed / 1000);
System.out.println("Number of times the simulation ran: " + numSimulationRan);
}
}
输出:
A new customer arrives at time 5
A new customer arrives at time 10
A new customer arrives at time 11
A new customer arrives at time 12
A new customer arrives at time 25
A new customer arrives at time 28
A new customer arrives at time 31
A new customer arrives at time 45
A new customer arrives at time 48
A new customer arrives at time 51
A new customer arrives at time 59
A new customer arrives at time 64
A new customer arrives at time 68
A new customer arrives at time 86
A new customer arrives at time 100
A new customer arrives at time 106
A new customer arrives at time 108
A new customer arrives at time 113
A new customer arrives at time 115
A new customer arrives at time 120
A new customer arrives at time 124
A new customer arrives at time 125
A new customer arrives at time 126
A new customer arrives at time 132
A new customer arrives at time 137
A new customer arrives at time 153
A new customer arrives at time 156
A new customer arrives at time 164
A new customer arrives at time 201
A new customer arrives at time 206
A new customer arrives at time 208
A new customer arrives at time 219
A new customer arrives at time 226
A new customer arrives at time 233
A new customer arrives at time 234
A new customer arrives at time 237
A new customer arrives at time 242
A new customer arrives at time 246
A new customer arrives at time 247
A new customer arrives at time 251
A new customer arrives at time 262
A new customer arrives at time 271
A new customer arrives at time 276
A new customer arrives at time 277
A new customer arrives at time 282
A new customer arrives at time 285
A new customer arrives at time 287
A new customer arrives at time 292
A new customer arrives at time 296
A new customer arrives at time 298
A new customer arrives at time 318
A new customer arrives at time 319
A new customer arrives at time 327
A new customer arrives at time 336
A new customer arrives at time 337
A new customer arrives at time 338
A new customer arrives at time 346
A new customer arrives at time 355
A new customer arrives at time 356
A new customer arrives at time 358
A new customer arrives at time 362
A new customer arrives at time 363
A new customer arrives at time 366
A new customer arrives at time 374
A new customer arrives at time 379
A new customer arrives at time 380
A new customer arrives at time 384
A new customer arrives at time 389
A new customer arrives at time 400
A new customer arrives at time 407
A new customer arrives at time 416
A new customer arrives at time 418
A new customer arrives at time 424
A new customer arrives at time 427
A new customer arrives at time 433
A new customer arrives at time 436
A new customer arrives at time 437
A new customer arrives at time 438
A new customer arrives at time 446
A new customer arrives at time 454
A new customer arrives at time 466
A new customer arrives at time 469
A new customer arrives at time 471
Average waiting time per customer: 1.0
Average longest waiting time: 2.714
Average number of minutes: 475.0
Longest waiting time: 8
Average number of customers 83.0
Number of times the simulation ran: 1000
Average waiting time per customer: 2.0
Average longest waiting time: 2.714
Average number of minutes: 950.0
Longest waiting time: 8
Average number of customers 166.0
Number of times the simulation ran: 2000
Average waiting time per customer: 3.0
Average longest waiting time: 2.714
Average number of minutes: 1425.0
Longest waiting time: 8
Average number of customers 249.0
Number of times the simulation ran: 3000
有个原则叫Don't Repeat Yourself or DRY。在这个词被创造出来之前,我们曾经开玩笑说懒惰是程序员的一种好品质:如果你必须做某事两次,那么你就经常做一次。
也许您可以考虑声明一些 methods,因为您的程序似乎有 3 次相同的代码。
讽刺的是,有一件事你只能做一次:初始化统计变量:
int numServed = 0;
...
如果您复制此部分并删除类型(因此显示为 numServed = 0;
等),然后再复制一次,它会重置这些变量。
但是,复制粘贴代码是一种不好的做法:现在您需要维护的代码是原来的 3 倍。
要解决此问题,您可以像这样构建程序:
class QueueSimulation
{
public static void main(String[] args) {
for ( int test = 0; test < 3; test ++ ) {
int numServed = 0;
..
for ( int i = 0; i < 1000; i++) {
...
}
System.out.println();
....
}
}
}
没有重复的代码,它对变量进行了本地化,因此它们在每次模拟时都被重置 - 这解决了您的问题。
这只是朝着正确方向迈出的一步。还有一个步骤是 factor out methods at abstraction levels, such as Top-Down and Bottom-Up design.
TLDR:一个好的经验法则是让任何方法适合一页文本。
在你的情况下:你是运行同样的模拟3次。该模拟的细节是什么与方法 运行 模拟 3 次无关。
这是另一个可能的大纲,我们将变量转换为 QueueSimulation
class 的字段。要初始化它们,我们只需实例化一个新的 QueueSimulation。这里是:
class QueueSimulation
{
public static void main(String[] args) {
for ( int test = 0; test < 3; test ++ ) {
QueueSimulation sim = new QueueSimulation();
sim.simulate();
sim.report();
}
}
Queue<Customer> q = new LinkedList<Customer>();
int numServed = 0;
...
int totalProcessingTime = 0;
public void simulate() {
for ( int i = 0; i < 1000 ) {
....
}
}
public void report() {
System.out.println( ....
...
}
}