C#While无限循环
我仍在学习C#的基础知识,目前正忙于编写一些代码,这些代码让我陷入了无限循环C#While无限循环,c#,loops,while-loop,infinite-loop,infinite,C#,Loops,While Loop,Infinite Loop,Infinite,我仍在学习C#的基础知识,目前正忙于编写一些代码,这些代码让我陷入了无限循环 do { Console.Write("Please enter the Exam mark for student " + (index + 1) + ": "); Exam[index] = double.Parse(Console.ReadLine()); Console.Write("Please enter the DP for student "
do
{
Console.Write("Please enter the Exam mark for student " + (index + 1) + ": ");
Exam[index] = double.Parse(Console.ReadLine());
Console.Write("Please enter the DP for student " + (index + 1) + ": ");
DP[index] = double.Parse(Console.ReadLine());
Final[index] = (DP[index] + Exam[index]) / 2;
index++;
} while (DP[index] != 999);
当我键入999时,代码不会中断。在检查
条件时,在之前将索引增加为1。我猜这个索引的值是0
解决方案可以是记住变量中的前一个值,或从索引中减去一个值:
} while (DP[index - 1] != 999);
另一个问题可能是使用double,因为它不精确,如果您输入该值,它并不总是与999
匹配。如果可以,使用int
或decimal
或使用while(DP[index++]!=999)简单地增加索引
代码>将在实际情况后执行。使用调试器,这样的问题可以在编写问题所需的较少时间内解决。