C# 用于未赋值变量错误

C# 用于未赋值变量错误,c#,C#,我不断得到未赋值变量的错误,下面是TotalAmount=TotalQuantity*UnitPrice。请告知 private void button1_Click(object sender, EventArgs e) { string client; string date; string telNumber; const decimal TaxRate = 0.43M; uint Quantity, Amount, Total; de

我不断得到未赋值变量的错误,下面是TotalAmount=TotalQuantity*UnitPrice。请告知

private void button1_Click(object sender, EventArgs e)
{
    string client;
    string date;
    string telNumber;

    const decimal TaxRate = 0.43M;

    uint Quantity, Amount, Total;

    decimal TotalOrder, TaxAmount, SalesTotal, TotalQuantity;
    decimal UnitPrice, AmountTended, Difference, TotalAmount;


    Console.SetCursorPosition(10, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(10, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(58, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    date = Console.ReadLine();

    Console.SetCursorPosition(67, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //TotalQuantity
    bool test = false;
    do
    {
        try
        {
            Console.SetCursorPosition(2, 12);
            Console.Write(" ");

            Console.SetCursorPosition(2, 12);
            TotalQuantity = Convert.ToDecimal(Console.ReadLine());
            test = false;
        }
        catch
        {
            test = true;
        }
    } while (test);

    //Item Description

    Console.SetCursorPosition(18, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //Unit Price

    bool test2 = false;
    do
    {
        try
        {
            Console.SetCursorPosition(47, 12);
            Console.Write(" ");

            Console.SetCursorPosition(47, 12);
            UnitPrice = Convert.ToDecimal(Console.ReadLine());
            test2 = false;
        }
        catch
        {
            test2 = true;
        }
    } while (test2);

    //Computations

    //TotalAmount
    **TotalAmount = TotalQuantity*UnitPrice;
    **

    Console.SetCursorPosition(65, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.Write("P ");
    Console.WriteLine(TotalQuantity*UnitPrice);
    Console.ReadLine();
}

根据C确定分配规则,无论是总量还是单价都不确定分配。你,作为一个人,能够看到代码并且知道它们是明确分配的。但是编译器不会分析变量test和test2的使用和赋值,因为它们与循环流有关。因此会出现编译时错误

解决这个问题的正确方法是创建一个helper方法来处理这些项的输入。这不仅可以确保您没有复制/粘贴的代码,还可以使用该方法的返回值为每个变量赋值,确保编译器可以判断该变量是否被明确赋值

例如:

private void button1_Click(object sender, EventArgs e)
{
    string client;
    string date;
    string telNumber;

    const decimal TaxRate = 0.43M;

    uint Quantity, Amount, Total;

    decimal TotalOrder, TaxAmount, SalesTotal, TotalQuantity;
    decimal UnitPrice, AmountTended, Difference, TotalAmount;


    Console.SetCursorPosition(10, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(10, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    client = Console.ReadLine();

    Console.SetCursorPosition(58, 5);
    Console.ForegroundColor = ConsoleColor.Yellow;
    date = Console.ReadLine();

    Console.SetCursorPosition(67, 7);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //TotalQuantity
    TotalQuantity = PromptDecimal(2);

    //Item Description

    Console.SetCursorPosition(18, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    telNumber = Console.ReadLine();

    //Unit Price    
    UnitPrice = PromptDecimal(47);

    //Computations

    //TotalAmount
    **TotalAmount = TotalQuantity*UnitPrice;
    **

    Console.SetCursorPosition(65, 12);
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.Write("P ");
    Console.WriteLine(TotalQuantity*UnitPrice);
    Console.ReadLine();
}
其中:

decimal PromptDecimal(int promptLine)
{
    while (true)
    {
        Console.SetCursorPosition(promptLine, 12);
        Console.Write(" ");

        Console.SetCursorPosition(promptLine, 12);

        decimal result;

        if (decimal.TryParse(Console.ReadLine(), out result))
        {
            return result;
        }
    }
}

您只为try/catch块内的那些变量赋值,而编译器不知道try块中的代码是否会在您稍后到达代码部分之前成功赋值,在那里您将值相乘

定义它们时为它们指定默认值

decimal UnitPrice = 0, TotalQuantity = 0;

这至少可以让您的程序编译。如果try块由于某些意外原因失败,那么将结束乘以两个零,程序将继续运行。根据您的情况,这可能是可取的,也可能是不可取的。

您是否使用**和**来计算总金额?我不同意这是重复的。是的,这是相同的错误消息。但是另一个问题涉及到一个真正未赋值的变量,而在这里,这只是编译器识别赋值的失败。此外,在另一个问题中,唯一合适的答案是初始化变量,而在这里,这些变量实际上不需要在实际分配为实数之前进行预初始化。将此问题视为另一个问题的副本是误导性的。在发布此问题之前,我确实检查了答案和问题,多亏了格兰特先生,我得到了答案,也谢谢你,皮特林爵士,我才一周大,在c中,我会尝试你的建议,并将其放在试抓之前,对吗?谢谢你,先生,成功了!!!非常感谢,分配默认值应该是处理此错误消息的最后手段。通常可以构造代码,以便编译器能够正确理解实际的执行流程,这样做通常会使代码更具表达力和可理解性。这是双赢的我会研究它是如何工作的,先生,再次谢谢,我是编程新手,我正在控制台应用程序上练习,谢谢,sirI也会尝试使用和研究这个,谢谢你,sirI,Promptline实际上做什么,先生?@Puchicha:你是说PromptDecimal吗?这是您将添加和使用的新方法,而不是您现在使用的循环。它执行与您的代码以前相同的操作,但在可重用方法中除外,该方法返回输入的十进制值。我将编辑答案,以便它提供有关我建议的更改的更多上下文。因此,主席先生,这意味着,如果我使用我制作的第一个代码,然后添加您的下一个代码,它也将以简单的方式生成相同的输出。谢谢您,先生。@Puchicha:您将更改您的原始代码,使其看起来与我在回答中所显示的相同,即删除循环,并替换为对该新方法的调用,然后您将添加该新方法本身。