【问题标题】:My compiler keeps saying I didn't assign my variable [duplicate]我的编译器一直说我没有分配我的变量[重复]
【发布时间】:2015-05-09 18:17:44
【问题描述】:

我正在尝试编写一个模拟赛车比赛的程序,用户插入比赛中的汽车数量和每辆车的时间。该程序将打印时间最快的汽车和第二快的汽车。

所以我写了这段代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int numc, first, second, time, i, temp;
            Console.WriteLine("Please insert the number of cars in the competition");
            numc = int.Parse(Console.ReadLine());
            Console.WriteLine("Please insert the time it took the car to finish the race");
            time = int.Parse(Console.ReadLine());
            first = time;
            for (i = 0; i < numc; i++)
            {
                Console.WriteLine("Please insert the time it took the car to finish the race");
                time = int.Parse(Console.ReadLine());
                if(time<first)
                {
                    temp=first;
                    first = time;
                    second = temp;
                }
            }
            Console.WriteLine("The time of the car who got first place is:" +first);
            Console.WriteLine("The time of the car who got second place is:" +second);
            Console.ReadLine();
        }
    }
}

我收到此错误:

使用未赋值的局部变量'second'

我不明白为什么会出现这个错误。

【问题讨论】:

  • 请显示您的完整堆栈跟踪
  • 它有助于显示与此处的帖子相关的哪一行代码导致错误。
  • C# 编译器不允许您使用未分配的局部变量。什么是你的for 从来不工作?编译器无法知道这一点。它没那么聪明。
  • @MoH。这也不例外。这是编译时出现的错误消息。

标签: c# unassigned-variable


【解决方案1】:

你只是在这个循环中分配second

if(time<first)
{
     temp=first;
     first = time;
     second = temp;
}

如果你不进去会发生什么?

如果你以后想使用它,你必须确保它被分配到任何地方。

【讨论】:

    【解决方案2】:

    你声明变量:

    int numc, first, second, time, i, temp;
    

    那么你可能分配它:

    for (i = 0; i < numc; i++)
    {
        // etc.
        if(time<first)
        {
            temp=first;
            first = time;
            second = temp;
        }
        // etc.
    }
    

    (或者您可能不会,这取决于运行时的条件或运行时 numc 的值。)

    那你就用它吧:

    Console.WriteLine("The time of the car who got second place is:" +second);
    

    如果if 条件的计算结果为false,会发生什么情况?或者如果for 循环没有迭代任何东西?然后在使用之前永远不会分配变量。这就是编译器告诉你的。

    如果您要始终使用该变量,那么您需要确保始终为其分配一些值。

    【讨论】:

      【解决方案3】:

      这里的问题是你的任务

      second = temp
      

      如果numc 输入小于一,则不会执行。

      由于编译器因此不能保证它已被分配,它会给你警告。

      在你的情况下,你可以做一些像分配这样的事情

      int second = 0;
      

      但您可能也想将Console.WriteLine 位更改为:

      if (numc > 0)
      {
          Console.WriteLine("The time of the car who got first place is:" +first);
          Console.WriteLine("The time of the car who got second place is:" +second);
      }
      else
      {
          Console.WriteLine("No cars were in the competition");
      }
      
      Console.ReadLine();
      

      【讨论】:

        【解决方案4】:

        这一行:

        Console.WriteLine("The time of the car who got second place is:" +second);
        

        numc &lt; 1time &gt;= first 时使用未赋值的second 变量。

        使用

        int second = 0;
        

        初始化这个字段。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-15
          • 2021-08-07
          • 1970-01-01
          相关资源
          最近更新 更多