我需要帮助来获得受尊重变量的输入值

时间:2014-02-20 06:16:00

标签: c#

我需要帮助才能从文本文件中获取输入并将这些值分配给适当的变量。我是c#的初学者。我不知道如何获得这些价值观。我希望任何人在while循环中填充其余的代码 文本数据的样本将是这样的 1 10 10 5 500 2 10 5 5 250 3 20 4 4 320

using System;
using System.IO;

namespace ReadInputDetails
{
    class Boxdetails
    {
        private string boxno;
        private double length;
        private double height;
        private double depth;
        private double volume;


    // Declare a number of box of type string:
    public string boxno
    {
        get 
        {
           return boxno; 
        }
        set 
        {
           boxno = value; 
        }
    }

    // Declare  properties of box of type double:
    public double length
    {
        get 
        { 
           return length; 
        }
        set 
        { 
           length = value; 
        }
    }
    public double height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }
    public double depth
        {
            get
            {
                return depth;
            }
            set
            {
                depth = value;
            }
        }
    public double volume
        {
            get
            {
                return volume;
            }
            set
            {
                volume = value;
            }
        }

        static void Main(string[] args)
        {
            try
            {
                 using (StreamReader sr = new StreamReader("c:/boxvalues.txt"))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {

                    }
                }
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您要将数据存储在boxvalues.txt中,请执行以下操作:

1 10 10 5 500
2 10 5 5 250

您可以检索以下值:

while ((line = sr.ReadLine()) != null)
{
    string[] Parts=line.Split(' ');
    boxno=Parts[0];  //1
    length=Convert.ToDouble(Parts[1]);  //10
    height=Convert.ToDouble(Parts[2]);  //10
    depth=Convert.ToDouble(Parts[3]);  //5
    volume=Convert.ToDouble(Parts[4]);  //500
}

详细了解字符串拆分here