我正在尝试进行一个涉及在c#
中执行while循环的计算然而,循环没有执行 - 代码的量子部分,我不明白为什么。
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变量是在程序中定义的,我不相信这些变量会导致问题。
答案 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
部分中的部分。在这种情况下,它不运行的唯一原因是三者中的一个:
noOfProcesses
小于1 timeelapsed
是&lt; = 0 由于您正在使用c#,并且希望使用VS,因此您应该使用调试器并逐步执行您的程序。