我正在制作一个非常简单的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();
答案 0 :(得分:0)
问题出在您的Writeline()
方法上。请以这种方式更改它:
Console.WriteLine($"The BMI of a person who weighs {userWeight} pounds and is {userHeight} inches tall has a BMI of {userAnswer}");
答案 1 :(得分:0)
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);
还有其他格式化字符串的方法,正如其他人所提到的那样,但这也足够:)。
答案 2 :(得分:0)
您可以尝试以下一种方法:打印出带有两位十进制数字的双精度字符。
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);