【问题标题】:Length calculator feet and inches长度计算器英尺和英寸
【发布时间】:2014-04-30 16:31:18
【问题描述】:

在我的 if 语句 (LengthCalculatorOption == 1) 中,我想将例如 187.96 厘米转换为英尺和英寸,例如 6 英尺 2 英寸。我怎么做?在我当前的代码中,它显示 6.17 英尺且始终为 0 英寸。我不知道为什么。

static void Main(string[] args) {

    double Centimetres = 0.0, Feet = 0.0, Inches = 0.0;
    string AnotherConversion = null;
    string LengthCalculatorMenu;
    int LengthCalculatorOption;

    do {
        LengthCalculatorMenu = ("Enter 1) Convert centimetres to feet and inches:"
                           +  "\nEnter 2) Convert feet and inches to centimetres:");
        Console.Write(LengthCalculatorMenu);
        LengthCalculatorOption = int.Parse(Console.ReadLine());

        if (LengthCalculatorOption == 1) {
            Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches");
            Centimetres = double.Parse(Console.ReadLine());

            Feet = (Centimetres / 2.54) / 12;
            Inches = (Centimetres / 2.54) - (Feet * 12);
            Centimetres = ((Feet * 12) + Inches) * 2.54;
            Console.WriteLine("\nThe equivalent in feet and inches is {0:C} ft {1:G} ins", Feet, Inches);

            Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
            AnotherConversion = Console.ReadLine();
        } else if (LengthCalculatorOption == 2) {
            Console.WriteLine("Please Enter the Feet");
            Feet = double.Parse(Console.ReadLine());
            Console.WriteLine("Please Enter the Inches");
            Inches = double.Parse(Console.ReadLine());

            Centimetres = ((Feet * 12) + Inches) * 2.54;
            Console.WriteLine("\nThe equivalent in centimetres is {0:G}cm", Centimetres);

            Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
            AnotherConversion = Console.ReadLine();
        } else {
            Console.WriteLine("\n\a\t Invalid Option!Option Must be 1 or 2");
        }
    } while (AnotherConversion == "y" || AnotherConversion == "Y");

【问题讨论】:

    标签: c# console-application calculator


    【解决方案1】:

    试试这个:

    Feet = (Centimetres / 2.54) / 12;
    int iFeet = (int)Feet;
    inches = (Feet - (double)iFeet) * 12;
    

    稍微详细一点:

    您将英尺定义为双精度值,这意​​味着它将是一个十进制值。因此,由于您要除以 12,它可以成为十进制表示。

    我的代码所做的是将英尺转换为整数(在这种情况下会将其四舍五入为 6)。然后我们减去双倍版本的 Feet(在这种情况下为 6.17),等于 0.17(余数)。我们将其乘以 12 以将 0.17 英尺转换为英寸

    编辑

    根据 Scott 的评论进行扩展,这将是一种不同的方式

    int totalInches = (Centimetres / 2.54); // This will take a floor function of Centimetres/2.54
    int Feet = (totalInches - totalInches % 12) / 12; // This will make it divisible by 12
    int inches = totalInches % 12; // This will give you the remainder after you divide by 12
    

    【讨论】:

    • 就个人而言,我更喜欢Math.Floor 而不是我自己使用隐式整数截断。那和我不喜欢明确地到处施放。另外,我觉得从上到下(英尺到英寸)而不是其他方式会导致精度/价值的潜在损失,尽管我很可能在最后一点上错了!
    • @Scott 你可能是正确的舍入错误的东西,修改后的答案也可以工作
    • 您的编辑将产生错误的英尺数。如果 totalInches 为 14,则 (14 - (14 % 12)) = 14 - 2 = 12。
    • @Scott Good catch :) 忘记将其除以 12 再次修复它
    【解决方案2】:

    保持为double,使用:

    double inp = 12.75; // e.g.
    
    double feet = Math.Floor(inp);
    double inches = (inp - feet) * 12.0;
    

    【讨论】:

      【解决方案3】:

      试试这个:

       double F = Math.Floor(Centimetres * 0.0328084);
       Feet = Centimetres * 0.0328084;
       Inches = (Feet - F) * 12;
      

      1 英尺 = 0.30480 米

      【讨论】:

        【解决方案4】:

        要以厘米为单位计算英尺和英寸的值,您可能需要这样做:

        double centimeters = 187.96;
        double inches = centimeters/2.54;
        double feet = Math.Floor(inches / 12);
        inches -= (feet*12);
        

        一般来说,一个人应该转换到最基本的水平,然后计算你的方式。这样,您只需进行一次转换工作,而不必重复转换计算。在本例中,我将厘米简单地转换为英寸,然后计算以英寸为单位的英尺数,然后从最终的英寸值中减去那么多。

        所以,如果我有 38 英寸,我将有 Math.Floor(38 / 12) 英尺或 3。然后 inches 将设置为 38 - (3*12),即 2,最终结果为 3 英尺,2英寸。

        【讨论】:

          【解决方案5】:

          一些通用方法:

          public static class Converter
          {
              public static (int Feet, double Inches) CentimetresToFeetInches(double centimetres)
              {
                  var feet = centimetres / 2.54 / 12;
                  var iFeet = (int)feet;
                  var inches = (feet - iFeet) * 12;
          
                  return (iFeet, inches);
              }
          
              public static string CentimetresToFeetInchesString(double centimetres, string footSymbol = " foot", string inchesSymbol = " inches")
              {
                  (var feet, var inches) = CentimetresToFeetInches(centimetres);
                  return $"{feet:N0}{footSymbol}, {inches:N0}{inchesSymbol}";
              }
          }
          

          用法:

          (var feet, var inches) = Converter.CentimetresToFeetInches(178);
          //feet == 5
          //inches == 10.078740157480315
          
          var feetInchesString = Converter.CentimetresToFeetInchesString(178);
          //5 foot, 10 inches
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-13
            相关资源
            最近更新 更多