c# - "Use of unassigned local variable"错误 C#,"while"命令

标签 c# while-loop

在编写代码时,我收到了 a、b 和 c 的错误。

           while(n==0)
        {
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            n=CheckTriang(a, b, c, n);

        }

        x=RightTriang(a, b, c, x);

我在循环中输入 a、b 和 c 的值,然后在工作方法 CheckTriang 中检查它们(确定这些值是否可以构成有效的三角形),然后打破条件,返回 n=1。 我认为问题可能出在编译器不确定循环是否会停止给出值。如何重写代码以使其更清晰?

提前致谢!

编辑: 我只复制了有问题的部分,而不是整个代码。声明了所需的变量。

最佳答案

切换到 do-while 循环:

do {
        a = Convert.ToInt32(Console.ReadLine());
        b = Convert.ToInt32(Console.ReadLine());
        c = Convert.ToInt32(Console.ReadLine());
        n=CheckTriang(a, b, c, n);
    } while (n == 0)

这保证您将至少完成一次循环。

关于c# - "Use of unassigned local variable"错误 C#,"while"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20010895/

相关文章:

c# - 为什么 DynamicProxy 的拦截器不会为 *each* 虚拟方法调用调用?

c# - 安全读取其元素同时更改的 long[] 内容的最快方法

c# - 为什么我的 WPF 应用程序在显示 WinForms 对话框后不能正常关闭?

c++ - 在无限循环 C++ 中定义一次变量

php - 如何对两个循环使用一个查询

c - 为什么我在运行这个程序时总是得到一个空表?

python - while、if 和 for 循环。 Python 上的第一个字母,最后一个字母游戏

c# - 内存流为空

c# - Visual Studio不会复制间接引用的项目中的内容文件

php - 如何在 PHP 中使用 while 循环按 ID 列出节中的数据?