gpt4 book ai didi

C# While 无限循环

转载 作者:行者123 更新时间:2023-11-30 20:36:26 25 4
gpt4 key购买 nike

我仍在学习 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 " + (index + 1) + ": ");
DP[index] = double.Parse(Console.ReadLine());

Final[index] = (DP[index] + Exam[index]) / 2;

index++;
} while (DP[index] != 999);

当我输入 999 时,密码没有中断。

最佳答案

在检查 while 条件之前,您将索引增加 1。我猜该索引的值是 0...

一个解决方案可以是记住变量中的前一个值,或者从索引中减去一个:

    } while (DP[index - 1] != 999);

另一个问题可能是 double 的使用,因为它不精确,如果您输入该值,它并不总是与 999 匹配。如果可以,请使用 intdecimal

关于C# While 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36979267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com