我无法遍历已分配给每个oracle队列的所有问题。所有问题都被成功添加,但我不能为我的生活让每个oracle回答每个问题,它已被分配1个1.意思是oracle 1回答了它的第一个问题,oracle 2回答了它的第一个问题,依此类推,直到所有队列都是空的并且所有问题都已得到解答。
/*
* Name: James Combs
* Course: 3345 Data Structures and Algorithms
* Description:
* This program assigns array queues to a number of oracles and loops through them in a
* round robin fashion, answering each question the oracle as in its queue.
*/
package OracleQueues;
import java.util.Iterator;
import java.util.Random;
public class Executor
{
public static void addQuestions (String[] questions, ArrayQueue[] oracles, int numOracles)
{
// Put the questions into a random oracles queue
for (int i = 0; i < questions.length; i++)
{
try
{
int rand = Utility.random(numOracles);
if (oracles[rand] == null)
{
System.out.println("Oracle " + (rand + 1) + " does not have a queue");
ArrayQueue queue = new ArrayQueue();
oracles[rand] = queue;
oracles[rand].enqueue(questions[i]);
System.out.println("Oracle " + (rand + 1) + " has added question ----(" + (i + 1) + ")" + questions[i] + "---- to its queue");
}
else
{
oracles[rand].enqueue(questions[i]);
System.out.println("Oracle " + (rand + 1) + " has added question ---- (" + (i + 1) + ")" + questions[i] + "---- to its queue");
}
}
catch (IllegalStateException e)
{
// advance to next oracle if this oracles queue is full, until questions have all been assigned.
System.out.println("This oracles queue is full, advancing to the next one...");
continue;
}
}
}
public static void main(String[] args)
{
Utility.init(); // initializes file readers
String[] questions = Utility.readQuestions(); // reads question.txt file into questions array
String[] answers = Utility.readAnswers(); // reads answers.txt file into answers array
int oCount = 0, qCount = 0;
int numOracles = answers.length; // finds the number of oracles
ArrayQueue[] oracles = new ArrayQueue[numOracles]; // initialize the number of oracles based on number of answers in the file
// 10 oracles
addQuestions(questions, oracles, numOracles);
System.out.println("\n\n");
// Loop through the oracles, having each one remove a question from its queue (if empty do nothing) and answer it with its unique answer (oracle[k] uses answers[k]). Do this repeatedly till all queues become empty.
// Create a list to hold the oracles
for(String q : questions)
{
if (oCount == 10)
{
oCount = 0;
}
if (oracles[oCount] == null || oracles[oCount].isEmpty())
{
continue;
}
String output = oracles[oCount].dequeue();
System.out.println("Oracle # " + (oCount + 1) + " ---- " + output + " ----> " + answers[oCount]);
oCount++;
}
/*for (int i = 0; i < questions.length; i++)
{
System.out.println("Answering question " + (i + 1) + "...");
for (int j = 0; j < numOracles; j++)
{
// if queue is empty or doesnt have one, then continue
if (oracles[j] == null || oracles[j].isEmpty())
{
continue;
}
// otherwise, dequeue and display question and corresponding answer
String output = oracles[j].dequeue();
System.out.println("Oracle # (" + (j + 1) + ") --- " + output + " ----> " + answers[j] + "\n");
}*/
}
}
答案 0 :(得分:0)
经过深思熟虑,问题已经解决了。我的ArrayQueue类
出现了一些问题