为什么会出现“使用未分配的局部变量”错误?

时间:2018-10-03 12:00:01

标签: c# console-application

第18行中的变量区域始终显示“未分配变量的使用”错误。我已经尝试将int行中的area = length * width放进去,但这还是行不通的,它只是使长度和宽度都没有分配。 :/这可能只是我真的很糟糕,但是有帮助

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

namespace Room
{
class Program
{
    public static void Main(string[] args)
    {
        string AreaString;
        int length;
        int width;
        int area;
        double persquarefoot = 3.25;
        double priceofcarpet = area * persquarefoot;
        Console.Write("Enter the length of the room: ");
        AreaString = Console.ReadLine();
        length = Convert.ToInt32(AreaString);
        Console.Write("Enter the width of the room: ");
        AreaString = Console.ReadLine();
        width = Convert.ToInt32(AreaString);
        area = length * width;
        Console.Write("The floor space is");
        Console.Write(area);
        Console.WriteLine(" square feet.");
        Console.Write("The cost of carpet in the room is $");
        Console.Write(persquarefoot);
        Console.WriteLine(" per square foot.");
        Console.Write("The cost of carpeting the whole room would be $");
        Console.Write(priceofcarpet);
        Console.WriteLine(".");
        Console.ReadKey();
    }
}
}

1 个答案:

答案 0 :(得分:1)

您应该在代码段下方移动

double priceofcarpet = area * persquarefoot;

计算面积后

area = length * width;