2016-06-23 239 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(); 
}