我想寫一個代碼,它會問一個問題客戶端變暗爲一個箱子,一旦客戶端回覆它會問你是否需要添加更多的箱子,如果客戶說是的我會如何重複相同的代碼?我想我需要在這裏添加一個循環,但我不知道如何重用相同的變量。我怎樣才能重用一個雙
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());
}
你是什麼意思,什麼變量?你有很多。 – OldProgrammer
只需在while循環外聲明你的l變量,因此當你進入循環時,你不會重新實例化l。然後在你的while循環中有類似l = l + double.Parse(Console.ReadLine()); – Roman
@RomanSidorov :)這是完全失敗分配的好方法。 –