【问题标题】:How to convert meters into feet and inches如何将米转换为英尺和英寸
【发布时间】:2016-02-02 03:58:30
【问题描述】:

创建一个程序,我一直在努力将米转换为英尺和英寸,但我想我终于让它工作了。

我现在的问题是变量 inchesleft 它是一个 int 并且我正在努力解决如何使它成为一个整数,因为我想删除其余的英寸值,这样我就可以得到一个 6 英尺 4 英寸等的值。

代码如下:

double inft, convert, inchesleft, value = 0.3048;
int ft;
string input;

Console.WriteLine("please enter amount of metres");
input = Console.ReadLine();
convert = double.Parse(input);

inft = convert / value;
ft = (int)inft;
inchesleft = convert / value % 1 *12;


Console.WriteLine("{0} feet {1} inches.", ft, inchesleft);
Console.ReadLine();

【问题讨论】:

  • “我现在的问题是变量inchesleft,它是一个int”——不,它不是......它是一个double。看看它在哪里被声明。 (顺便说一句,我强烈建议在第一次使用时声明变量,而不是一开始就声明所有内容。)

标签: c#


【解决方案1】:

试试这个:

double inft, convert, value = 0.3048;
int ft, inchesleft;
string input;

Console.WriteLine("please enter amount of metres");
input = Console.ReadLine();
convert = double.Parse(input);

输入数字除以0.3048得到英尺

inft = convert / value;

现在我们得到十进制的英尺。获取英尺的左边部分(小数点前)

ft = (int)inft;

取英尺的右边部分(小数点后)除以 0.08333 转换成英寸

double temp = (inft - Math.Truncate(inft)) / 0.08333; 

现在我们得到十进制英寸。取英寸的左边部分(小数点前)

inchesleft = (int)temp; // to be more accurate use temp variable which contains the decimal point value of inches 

Console.WriteLine("{0} feet {1} inches.", ft, inchesleft);
Console.ReadLine();

【讨论】:

    【解决方案2】:

    我使用了上面@Waqar Ahmed 方法的某些部分并对其进行了微调。谢谢大佬。

    //-----METHODS
            //VARIABLES
            double inchFeet;
            int wholeFeet;
    
        public double GetHeightFeet()
        {
            //CENTIMETERS TO FEET
            //PlayerHeight is ___cm input
            inchFeet = (PlayerHeight / 0.3048) / 100;
            //LEFT PART BEFORE DECIMAL POINT. WHOLE FEET
            wholeFeet = (int)inchFeet;
            return wholeFeet;
        }
    
        public double GetHeightInches()
        {
            //DECIMAL OF A FOOT TO INCHES TEST HEIGHT 181cm to see if 11''
            double inches = Math.Round((inchFeet - wholeFeet) / 0.0833);
            return inches;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2023-01-18
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多