【问题标题】:What does "Use of unassigned local variable" mean?“使用未分配的局部变量”是什么意思?
【发布时间】:2011-04-19 01:03:29
【问题描述】:

AnnualRate、monthlyCharge 和 lateFee 不断出现此错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab_5___Danny_Curro
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstName;
            string lastName;
            int accNumber;
            string creditPlan;
            double balance;
            string status;
            Boolean late = false;
            double lateFee;
            double monthlyCharge;
            double annualRate;
            double netBalance;


            Console.Write("Enter First Name: ");
            firstName = Console.ReadLine();

            Console.Write("Enter Last Name: ");
            lastName = Console.ReadLine();

            Console.Write("Enter Account Number: ");
            accNumber = Convert.ToInt32(Console.ReadLine());


            Console.Write("Enter Credit Card Plan Number[Blank Will Enter Plan 0]: ");
            creditPlan = Console.ReadLine();

            Console.Write("Enter Balance: ");
            balance = Convert.ToDouble(Console.ReadLine());

            Console.Write("Is This Account Late?: ");
            status = Console.ReadLine().Trim().ToLower();

            if (creditPlan == "0")
            {
                annualRate = 0.35;  //35%
                lateFee = 0.0;
                monthlyCharge = balance * (annualRate * (1 / 12));
                return;
            }

            if (creditPlan == "1")
            {
                annualRate = 0.30;  //30%
                if (status == "y")
                {
                    late = true;
                }

                else if (status == "n")
                {
                    late = false;
                }
                if (late == true)
                {
                    lateFee = 25.00;
                }
                monthlyCharge = balance * (annualRate * (1 / 12));
                return;
            }
            if (creditPlan == "2")
            {
                annualRate = 0.20;  //20%
                if (status == "y")
                {
                    late = true;
                }

                else if (status == "n")
                {
                    late = false;
                }
                if (late == true)
                {
                    lateFee = 35.00;
                }
                if (balance > 100)
                {
                    monthlyCharge = balance * (annualRate * (1 / 12));
                }
                else
                {
                    monthlyCharge = 0;
                }
                return;
            }
            if (creditPlan == "3")
            {
                annualRate = 0.15;  //15%
                lateFee = 0.00;

                if (balance > 500)
                {
                    monthlyCharge = (balance - 500) * (annualRate * (1 / 12));
                }
                else
                {
                    monthlyCharge = 0;
                }
                return;
            }
            netBalance = balance - (lateFee + monthlyCharge);


            Console.WriteLine("Name: \t\t\t {0}  {1}", firstName, lastName);
            Console.WriteLine("Account Number: \t{0}", accNumber);
            Console.WriteLine("Credit Plane: \t\t{0}",creditPlan);
            Console.WriteLine("Account Late: \t\t{0}", late);
            Console.WriteLine("Balance: \t\t{0}", balance);
            Console.WriteLine("Late Fee: \t\t{0}", lateFee);
            Console.WriteLine("Interest Charge: \t{0}", monthlyCharge);
            Console.WriteLine("Net Balance: \t\t{0}",netBalance);
            Console.WriteLine("Annual Rate: \t\t{0}", annualRate);
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 我不建议用两个if 替换if-else 语句。这不是最好的方法。您是否想过如果在 if 语句中更改 creditPlan 的值会发生什么?或者如果你调用一个改变它的函数(使它不那么明显)?然后执行可以进入下一个if,我不是你的意思。
  • 我在提交这个程序之前重新编写了 if 语句。谢谢你的帮助,我不会想到这一点。谢谢
  • 相关帖子here 在回答线程中提供了有趣的细节。

标签: c# unassigned-variable


【解决方案1】:

编译器不够聪明,无法知道至少有一个if 块将被执行。因此,无论如何都不会分配像annualRate 这样的变量。以下是让编译器理解的方法:

if (creditPlan == "0")
{
    // ...
}
else if (creditPlan == "1")
{
    // ...
}
else if (creditPlan == "2")
{
    // ...
}
else
{
    // ...
}

编译器知道使用 if/else 块,保证执行其中一个块,因此如果您在所有块中分配变量,它不会给编译器错误。

顺便说一句,您还可以使用switch 语句而不是ifs 来使您的代码更简洁。

【讨论】:

  • 非常感谢,这真的很有帮助,我很想感谢大家,但没有时间评论大家的回复。所以谢谢
  • 聚会有点晚了,但如果有意义的话,您也可以使用默认值实例化 AnnualRate。
【解决方案2】:

将您的声明更改为:

double lateFee = 0.0;
double monthlyCharge = 0.0;
double annualRate = 0.0;

导致该错误的原因是,至少有一条路径通过您的代码,这些变量最终没有被设置为任何值。

【讨论】:

    【解决方案3】:

    因为如果没有一个 if 语句的计算结果为 true,那么局部变量将被取消分配。在那里抛出一个 else 语句,并为这些变量分配一些值,以防 if 语句不评估为真。如果这不会使错误消失,请在此处发回。

    您的另一个选择是在代码开头声明变量时将变量初始化为某个默认值。

    【讨论】:

      【解决方案4】:

      给他们一个默认值:

      double lateFee=0.0;
      double monthlyCharge = 0.0;
      double annualRate = 0.0;
      

      基本上,所有可能的路径都不会初始化这些变量。

      【讨论】:

        【解决方案5】:

        使用关键字“默认”!!!

            string myString = default;
            double myDouble = default;
        
            if(!String.IsNullOrEmpty(myString))
               myDouble = 1.5;
        
            return myDouble;
        

        【讨论】:

          【解决方案6】:

          你的代码中有很多路径没有初始化你的变量,这就是编译器抱怨的原因。

          具体来说,您没有验证creditPlan 的用户输入 - 如果用户输入的值不是"0","1","2" or "3",则不会执行任何指示的分支(并且creditPlan 不会默认为根据您的用户提示为零)。

          正如其他人所提到的,编译器错误可以通过在检查分支之前对所有派生变量进行默认初始化来避免,或者确保至少执行一个分支(即,分支的互斥性,与else 声明失败)。

          不过,我想指出其他潜在的改进:

          • 先验证用户输入,然后再将其用于您的代码。
          • 将参数建模为一个整体 - 每个计划都有多个适用的属性和计算。
          • 为数据使用更合适的类型。例如CreditPlan 似乎有一个有限域,比string 更适合enumerationDictionary。财务数据和百分比应始终建模为 decimal,而不是 double,以避免四舍五入问题,并且“状态”似乎是一个布尔值。
          • 干掉重复的代码。计算,monthlyCharge = balance * annualRate * (1/12)) 对多个分支是通用的。出于维护原因,请勿复制此代码。
          • 可能更高级,但请注意,函数现在是 C# 的一等公民,因此您可以将函数或 lambda 分配为属性、字段或参数!。

          例如这是您的模型的另一种表示形式:

              // Keep all Credit Plan parameters together in a model
              public class CreditPlan
              {
                  public Func<decimal, decimal, decimal> MonthlyCharge { get; set; }
                  public decimal AnnualRate { get; set; }
                  public Func<bool, Decimal> LateFee { get; set; }
              }
          
              // DRY up repeated calculations
              static private decimal StandardMonthlyCharge(decimal balance, decimal annualRate)
              { 
                 return balance * annualRate / 12;
              }
          
              public static Dictionary<int, CreditPlan> CreditPlans = new Dictionary<int, CreditPlan>
              {
                  { 0, new CreditPlan
                      {
                          AnnualRate = .35M, 
                          LateFee = _ => 0.0M, 
                          MonthlyCharge = StandardMonthlyCharge
                      }
                  },
                  { 1, new CreditPlan
                      {
                          AnnualRate = .30M, 
                          LateFee = late => late ? 0 : 25.0M,
                          MonthlyCharge = StandardMonthlyCharge
                      }
                  },
                  { 2, new CreditPlan
                      {
                          AnnualRate = .20M, 
                          LateFee = late => late ? 0 : 35.0M,
                          MonthlyCharge = (balance, annualRate) => balance > 100 
                              ? balance * annualRate / 12
                              : 0
                      }
                  },
                  { 3, new CreditPlan
                      {
                          AnnualRate = .15M, 
                          LateFee = _ => 0.0M,
                          MonthlyCharge = (balance, annualRate) => balance > 500 
                              ? (balance - 500) * annualRate / 12
                              : 0
                      }
                  }
              };
          

          【讨论】:

            【解决方案7】:

            您的分配都嵌套在条件 if 块中,这意味着它们有可能永远不会被分配。

            在类的顶部,将它们初始化为 0 或其他值

            【讨论】:

              【解决方案8】:

              编译器表示,如果 CreditPlan 未被识别,那么每年的利率将没有值。

              在创建局部变量(annualRate、monthlyCharge 和 lateFee)时,为它们分配一个默认值 (0)。

              此外,如果信用计划未知,您应该显示错误。

              【讨论】:

                【解决方案9】:

                并非所有代码路径都为lateFee 设置值。您可能希望在顶部为其设置默认值。

                【讨论】:

                  【解决方案10】:

                  您不会在 if 语句之外分配值......而且正如 @iomaxx 所指出的,信用可能不是 0、1、2 或 3。

                  尝试将单独的 if 语句更改为单个 if/else if/else if/else。或者在顶部指定默认值。

                  【讨论】:

                    【解决方案11】:

                    如果你像这样声明变量“annualRate”

                    类程序 {

                    **static double annualRate;**
                    
                    public static void Main() {
                    

                    试试吧..

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2022-06-15
                      • 2011-09-06
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-04-21
                      • 2013-07-21
                      • 1970-01-01
                      • 2016-03-25
                      相关资源
                      最近更新 更多