2016-06-23 170 views
-3

我想写一个代码,它会问一个问题客户端变暗为一个箱子,一旦客户端回复它会问你是否需要添加更多的箱子,如果客户说是的我会如何重复相同的代码?我想我需要在这里添加一个循环,但我不知道如何重用相同的变量。我怎样才能重用一个双

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()); 
} 
+2

你是什么意思,什么变量?你有很多。 – OldProgrammer

+0

只需在while循环外声明你的l变量,因此当你进入循环时,你不会重新实例化l。然后在你的while循环中有类似l = l + double.Parse(Console.ReadLine()); – Roman

+1

@RomanSidorov :)这是完全失败分配的好方法。 –

回答

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())); 
      } 
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; 
    } 

}

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(); 
}