Вы никогда в настоящее время устанавливая значения, которые вы читаете из командной строки в своих Console.ReadLine()
вызовах.
Console.ReadLine()
фактически вернет строку, представляющую введенное значение, поэтому вам нужно будет преобразовать ее в правильный числовой тип, чтобы вы могли его использовать.
Поскольку вы имеете дело с целыми числами, вы можете использовать методы Convert.ToInt32()
или Int32.Parse()
, как показано ниже:
Console.Write("Enter your Length: ");
// Read and parse your length
int length = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your Width: ");
// Read and parse your width
int width = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your Height: ");
// Read and parse your height
int height = Convert.ToInt32(Console.ReadLine());
// Now you have all of your dimensions so calculate
int totalDims = length * width * height;
// Since you are performing division, you could have a fractional
// value here, so you might want to use another type like decimal
decimal cubicFeet = totalDims/1728m;
// Output your result
Console.WriteLine("Your total cubic feet is " + cubicFeet);
Если вы хотите imlpement еще более безопасное решение, следует рассмотреть метод Int32.TryParse()
который позволит вам знать, если преобразование хорошо или нет, и позволит вам снова подскажете:
int length, width, height, totalDims;
Console.Write("Enter your Length: ");
// Read and parse your length
while(!Int32.TryParse(Console.ReadLine(), out length)){
Console.Write("Enter your Length again: ");
}
Console.Write("Enter your Width: ");
// Read and parse your width
while(!Int32.TryParse(Console.ReadLine(), out width)){
Console.Write("Enter your Width again: ");
}
Console.Write("Enter your Height: ");
// Read and parse your height
while(!Int32.TryParse(Console.ReadLine(), out height)){
Console.Write("Enter your Height again: ");
}
// Now you have all of your dimensions so calculate
totalDims = length * width * height;
// Since you are performing division, you could have a fractional
// value here, so you might want to use another type like decimal
decimal cubicFeet = totalDims/1728m;
// Output your result
Console.WriteLine("Your total cubic feet is " + cubicFeet);
вы можете see a complete working example of this in action here.
Письмо приведет к исключению формата. –