我怎样才能重复使用double

时间:2016-06-23 00:06:03

标签: c#

我正在尝试编写一个代码,当客户回复它时,会询问客户端为箱子设置的问题,如果客户端说是,我将如何重复相同的代码?我想我需要在这里添加一个循环,但我不确定如何重用相同的变量。

Console.WriteLine("Please enter the crate Length for your incoming shipment: ");
double l = new double();
l = double.Parse(Console.ReadLine());

Console.WriteLine("Enter the crate Width for your incoming shipment: ");
double w = new double();
w = double.Parse(Console.ReadLine());

Console.WriteLine("Enter the crate Height for your incoming shipmet");
double h = new double();
h = double.Parse(Console.ReadLine());

double totalDims = new double();
totalDims = l * w * h;
double volKg = new double();
volKg = totalDims / 366;

Console.WriteLine("Your total Vol Kg is {0:0.00}", +volKg);
Console.ReadLine();

Console.Write("Are there any additional crates y/n? ");
char a = new char();
a = char.Parse(Console.ReadLine());

char y = default(char);
while (a == y)
{
    //Crate Dimensions Entered
    Console.WriteLine("Please enter the crate Length for your incoming shipment: ");
    double l = new double();
    l = double.Parse(Console.ReadLine());

    Console.WriteLine("Enter the crate Width for your incoming shipment: ");
    double w = new double();
    w = double.Parse(Console.ReadLine());

    Console.WriteLine("Enter the crate Height for your incoming shipmet");
    double h = new double();
    h = double.Parse(Console.ReadLine());
}

3 个答案:

答案 0 :(得分:1)

由于您可能有各种各样的问题,因此您应该使用List(Of T)而不是单个变量。 (或者最好创建一个名为Dimension的类,其中包含LenghtBreadthHeight等属性。)

这样的事可能会对你有所帮助:

            Console.Write("Are there any additional crates y/n? ");
            char a = new char();
            a = char.Parse(Console.ReadLine());

            dim l as new List(Of double)()
            dim w as new List(Of double)()
            dim h as new List(Of double)()

            char y = default(char);
            while (a == y)  '' Revise this condition as it takes `a` input once and keep looping on it, instead it should take input always unless user enters `n`
            {
                //Crate Dimensions Entered
                Console.WriteLine("Please enter the crate Length for your incoming shipment: ");
                l.Add(double.Parse(Console.ReadLine()));

                Console.WriteLine("Enter the crate Width for your incoming shipment: ");
                w.Add(double.Parse(Console.ReadLine()));

                Console.WriteLine("Enter the crate Height for your incoming shipmet");
                h.Add(double.Parse(Console.ReadLine()));
            }

答案 1 :(得分:0)

这就是我要做的事情

我还在输入中添加了一些验证

    public class CrateDimensions
{
    public double Height { get; set; }
    public double Width { get; set; }
    public double Length { get; set; }
}
class Program
{
    static List<CrateDimensions> crates = new List<CrateDimensions>();

    static void Main(string[] args)
    {
        GetCrateDim();
    }

    static void GetCrateDim()
    {
        double l = GetDim("Please enter the crate Length for your incoming shipment");
        double w = GetDim("Please enter the crate Width for your incoming shipment");
        double h = GetDim("Please enter the crate Height for your incoming shipment");

        crates.Add(new CrateDimensions() { Height = h, Length = l, Width = w });
        double totalDims = new double();
        totalDims = l* w * h;
        double volKg = new double();
        volKg = totalDims / 366;

        Console.WriteLine("Your total Vol Kg is {0:0.00}", +volKg);

        Console.Write("Are there any additional crates y/n? ");
        char a = new char();
        a = char.Parse(Console.ReadLine().ToLower());
        if (a == 'y')
            GetCrateDim();
    }

    static double GetDim(string message)
    {
        Console.WriteLine(message + ": ");
        double dimLen = 0;
        while (!double.TryParse(Console.ReadLine(), out dimLen))
            Console.WriteLine("Please enter a valid number");
        return dimLen;
    }

}

答案 2 :(得分:0)

这里的代码是我能想到的最简单的形式:

char a = 'y';
while (char.ToLower(a) == 'y')
{
    Console.WriteLine("Please enter the crate Length for your incoming shipment:");
    double l = double.Parse(Console.ReadLine());

    Console.WriteLine("Enter the crate Width for your incoming shipment:");
    double w = double.Parse(Console.ReadLine());

    Console.WriteLine("Enter the crate Height for your incoming shipment:");
    double h = double.Parse(Console.ReadLine());

    double totalDims = l * w * h;
    double volKg = totalDims / 366;

    Console.WriteLine("Your total Vol Kg is {0:0.00}", volKg);
    Console.WriteLine();

    Console.WriteLine("Are there any additional crates y/n? ");
    a = char.Parse(Console.ReadLine());
    Console.WriteLine();
}