我正在制作一个非常简单的BMI计算器,我已经工作了,但不得不更改舍入问题,现在我遇到了"预期的方法名称",用于userWeight和userHeight的最终输出。这是代码。
double userWeight;
double userHeight;
double userAnswer;
Console.WriteLine("Welcome to our program for calculating Body Mass Index");
Console.WriteLine("Please enter your weight in pounds.");
userWeight = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter your height in inches.");
userHeight = double.Parse(Console.ReadLine());
userAnswer = (userWeight / (userHeight * userHeight) * 703);
Console.WriteLine("The BMI of a person who weighs ") + userWeight ("pounds and is ") + userHeight ("inches tall has a BMI of ") + userAnswer;
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Console.WriteLine 的格式有点不对劲。 你写道:
Console.WriteLine("The BMI of a person who weighs ") + userWeight ("pounds and is ") + userHeight ("inches tall has a BMI of ") + userAnswer;
但你想做的是这样的:
Console.WriteLine("The BMI of a person who weighs " + userWeight + " pounds and is " + userHeight + " inches tall has a BMI of " + userAnswer);
正如其他人提到的,还有其他方法可以格式化字符串,但这在:)也足够了。
问题出在您的Writeline()
方法上。请按以下方式更改它:
Console.WriteLine($"The BMI of a person who weighs {userWeight} pounds and is {userHeight} inches tall has a BMI of {userAnswer}");
你可以试试这个: 这将打印带有两个十进制数字的双精度。
Console.WriteLine(string.Format("The BMI of a person who weighs {0:0.00} pounds and is {1:0.00} inches tall has a BMI of {2:0.00}", userWeight, userHeight, userAnswer));
其他简单的方法是:
Console.WriteLine("The BMI of a person who weighs " + userWeight + " pounds and is " + userHeight + " inches tall has a BMI of " + userAnswer);