我正在学习C#,我想用C#做简单的数学问题来提高我的技能。有人能告诉我这段代码中缺少什么吗?我希望用户输入特定的长度,宽度和高度,然后代码将乘以它然后除以1728然后显示但它不能正常工作。有人可以帮助我,谢谢你
Console.Write("Enter your Length: ");
int length = new int();
Console.ReadLine();
Console.Write("Enter your Width: ");
int width = new int();
Console.ReadLine();
Console.Write("Enter your Height: ");
int height = new int();
Console.ReadLine();
int totalDims = new int();
totalDims = length * width * height;
int cubicFeet = new int();
cubicFeet = totalDims/1728;
Console.WriteLine("Your total cubic feet is " + cubicFeet);
Console.ReadLine();
答案 0 :(得分:1)
您必须解析从控制台读取的行int
:
int length = 0;
int.TryParse(Console.ReadLine(), out length);
...
int width = 0;
int.TryParse(Console.ReadLine(), out width);
...
int height = 0;
int.TryParse(Console.ReadLine(), out height);
另外,如果您希望cubicFeet
为浮动数字:
double cubicFeet = totalDims / 1728.0;
答案 1 :(得分:1)
您目前从未在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);
如果您想要实现更安全的解决方案,您应该考虑使用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);
答案 2 :(得分:0)
1。)你必须设置你的号码。 Console.ReadLine()返回您在控制台中输入的输入的字符串值,因此您必须将其转换为int。
2。)最后的操作可能会返回一个小数,所以totalDims和cubicFeet是十进制。
Console.Write("Enter your Length: ");
int length = new int();
length = int.Parse(Console.ReadLine());
Console.Write("Enter your Width: ");
int width = new int();
width = int.Parse(Console.ReadLine());
Console.Write("Enter your Height: ");
int height = new int();
height = int.Parse(Console.ReadLine());
int totalDims = new int();
totalDims = length * width * height;
//isnt cubic feet just length * width * height?
//decimal cubicFeet = new decimal();
//cubicFeet = totalDims;
Console.WriteLine("Your total cubic feet is " + totalDims);
Console.ReadLine();