为什么这个循环增加一个变量?

时间:2016-12-30 22:35:46

标签: c# loops

我正在尝试进行一个涉及在c#

中执行while循环的计算
  • 我想执行循环并减去remainingTime数组中的量子并将新值分配给timeelapsed。
  • 我的循环应该执行,而timeelapsed是> 0,一旦达到0就停止

然而,循环没有执行 - 代码的量子部分,我不明白为什么。

 static void Main(string[] args)
    {
         int quantum, noOfProcesses, processNumber = 0; // noOfprocesses variable is to determine array sizes, processNumber tells user what process number they input
         int timeelapsed, count = 0;

         Console.WriteLine("Please enter the number of processes:");

         noOfProcesses = Convert.ToInt32(Console.ReadLine()); // allows numerical input 
         int[] bursttime = new int[noOfProcesses];
         int[] arrivalTime = new int[noOfProcesses];
         int[] remainingTime = new int[noOfProcesses];

         for (int i = 0; i < noOfProcesses; i++)
         {
             Console.WriteLine("Enter Burst time for process #" + processNumber);
             bursttime[i] = Convert.ToInt32(Console.ReadLine()); 
             processNumber++;     
         }

         processNumber = 1; // resets the process number for user interface 

         for (int i = 0; i < noOfProcesses; i++)
         {
             Console.WriteLine("Enter arrival time for process #" + processNumber);
             arrivalTime[i] = Convert.ToInt32(Console.ReadLine());
             processNumber++;
         }

         Console.WriteLine("Enter the time quantum:");
         quantum = Convert.ToInt32(Console.ReadLine()); // allows numerical input

        // calculations

        processNumber = 0;


        if (count <= noOfProcesses)
        {
            // remainingTime[0] = bursttime[0] + arrivalTime[0]; // burst + arrival = remaining time

            for (int i = 0; i < noOfProcesses; i++)
            {         
                do
                {
                    remainingTime[i] = bursttime[i] + arrivalTime[i]; // burst + arrival = remaining time
                    timeelapsed = remainingTime[i] - quantum; // - the quantum gives whats remaining
                    timeelapsed = timeelapsed - quantum;  
                }
                while (timeelapsed > 0);

                Console.WriteLine("#" + processNumber + " Time taken: " + timeelapsed + "ms");
                processNumber++;
                count++;
                //timeelapsed = bursttime[i] + arrivalTime[i];

            }
        }

       // Console.WriteLine(arrivalTime[j]);
        Console.ReadLine();


    }

请注意,noOfProcesses和processNumber变量是在程序中定义的,我不相信这些变量会导致问题。

2 个答案:

答案 0 :(得分:2)

您在此循环中所做的一切都是一遍又一遍地重新计算相同的值:

do {
   remainingTime[i] = bursttime[i] + arrivalTime[i]; // burst + arrival = remaining time
   timeelapsed = remainingTime[i] - quantum; // - the quantum gives whats remaining
   timeelapsed = timeelapsed - quantum;  
} while (timeelapsed > 0);

例如,如果您使用实数,您可能会看到这个:

do {
   remainingTime = 100 + 100;
   timeelapsed = 200 - 10
   timeelapsed = 190 - 10;
} while (timeelapsed > 0);

循环永远不会结束。我不确定你的目标是什么,但你可能需要保持一个总计,例如:

remainingTime[i] += bursttime[i] + arrivalTime[i];

或者

remainingTime[i] -= bursttime[i] + arrivalTime[i];

答案 1 :(得分:-1)

这实际上是调试,很难在没有代码的情况下决定。我假设'量子'代码是循环的do部分中的部分。在这种情况下,它不运行的唯一原因是三者中的一个:

  1. noOfProcesses小于1
  2. timeelapsed是&lt; = 0
  3. 它正在进行循环的第一次迭代,但抛出异常。
  4. 由于您正在使用c#,并且希望使用VS,因此您应该使用调试器并逐步执行您的程序。