2015-02-24 1 views
0

Je rencontre des difficultés pour boucler toutes mes questions qui ont été assignées à la file d'attente de chaque oracle. Toutes les questions sont ajoutées avec succès, mais je ne peux pas pour la vie de moi chaque oracle répondre à chaque question qui lui a été assignée 1 par 1. Signification que l'oracle 1 répond à sa première question, oracle 2 répond à ses premières questions et ainsi de suite , jusqu'à ce que toutes les files d'attente soient vides et que toutes les questions aient été répondues.Round Robin S'il vous plaît enseignez-moi

/* 
* 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"); 
      }*/ 

     } 
} 
+1

Pourquoi faites-vous le placement aléatoire en premier lieu? Round robin est littéralement juste une boucle pour les files d'attente. –

+0

Cela fait partie de la simulation d'un processus de planification. Je l'ai compris finalement. Merci. Il y avait quelque chose qui ne va pas dans ma classe ArrayQueue –

Répondre

0

Questions a été résolu après beaucoup de réflexion. Quelque chose ne fonctionnait pas correctement avec ma classe ArrayQueue