在英尺和英寸中添加两个高度测量值

时间:2013-12-26 06:01:31

标签: java

我有两个浮点值5.9f9.11f的输入,它是5英尺9英寸和9英尺11英寸。我需要添加它们并显示15英尺8英寸。

public static void addingFeets(float a,float b)
{
    int c = (int)a*12;      
    int d = (int)b*12;      
    float e = Math.round((a%1)*10);     
    float f = Math.round((b%1)*10);     
    float g = (int) ((int)e+f);     
    int h = (int) (c+d+g);    
    System.out.println( h/12+" feet "+h%12+" inches");          
}

但当其中一个输入包含11英寸时,我无法获得正确的输出。

7 个答案:

答案 0 :(得分:2)

5英尺9英寸不能表示为5.9f,因为1英尺!= 10英寸。

更好的解决方案是在高处进行OO建模 - 正确的代码和漂亮的代码:

class Height {
    private int feet;
    private int inches;

    public Height(int feet, int inches) {
        this.feet = feet;
        this.inches = inches;
    }

    public static Height fromInches(int inches) {
        //Convert to feet and inches.
        return new Height(inches / 12, inches % 12);
    }

    public int toInches(){
        return feet * 12 + inches;
    }

    public Height addTo(Height another){
        //1. Convert both the heights to inches
        //2. Do simple addition of inches
        //3. Convert it back to Height using Height.fromInches, and return it
        // or, in other words (behold your eyes):

        return Height.fromInches(this.toInches() + another.toInches());
    }

    //Bonus        
    public String toString() {
        return String.format("%s'%s\"", feet, inches);
    }
}

现在,你可以说:

    Height height1 = new Height(5, 9);
    Height height2 = new Height(9, 11);
    System.out.println(height1.addTo(height2));

将很好地打印:

15'8"

答案 1 :(得分:1)

尝试这样的事情

private static int inchesFromString(String in) {
  if (in != null) {
    String[] a = in.split("\\.");
    int feet = (a.length > 0) ? Integer
        .valueOf(a[0]) : 0;
    int inches = (a.length > 1) ? Integer
        .valueOf(a[1]) : 0;
    return (feet * 12) + inches;
  }
  return -1;
}

public static void addingFeets(float a, float b) {
  String one = Float.toString(a);
  String two = Float.toString(b);
  int h = inchesFromString(one) + inchesFromString(two);
  System.out.println(h / 12 + " feet " + h % 12
      + " inches");
}
public static void main(String[] args) {
  addingFeets(5.9f, 9.11f);
}

当我运行它时,输出 -

15 feet 8 inches

答案 2 :(得分:0)

尝试这个(使用JDK类来处理数字):

    float a = 5.9f, b = 9.11f;
    String one = Float.toString(a);

    BigDecimal bd1 = new BigDecimal (one);
    BigDecimal r1 = bd1.remainder(BigDecimal.ONE);
    BigDecimal r2 = r1.movePointRight(r1.scale());
    int aInches = r2.intValue();
    int aFeet = bd1.intValue ();
    int aTot = (aFeet * 12) + aInches;

    String two = Float.toString(b);
    BigDecimal bd2 = new BigDecimal (two);
    BigDecimal br1 = bd2.remainder(BigDecimal.ONE);
    BigDecimal br2 = br1.movePointRight(br1.scale());
    int bInches = br2.intValue();

    int bFeet = bd2.intValue ();
    int bTot = (bFeet * 12) + bInches;

    int h = aTot + bTot;
    System.out.println( h/12+" feet "+h%12+" inches");      

答案 3 :(得分:0)

如果我理解正确:你似乎将0.11视为0英尺11英寸。

但是,然后Math.round会舍入到最接近的十进制值,所以:

Math.round((.11%1)*10) = 
Math.round(1.1)=
1

尝试:

e = Math.round( (a%1) * 10 ) + Math.round( ((a*10)%1) * 10 )

然而,这会引起一个问题:0.1 = 0.10,那么是一英寸还是十英寸?我建议你使用0.01英尺1英寸。然后你有:

e = (a%1)*100

答案 4 :(得分:0)

使用Lombok @Data构造函数,getter,setter和toString

package com.stackoverflow.so20779885;

import lombok.Data;

public class App {
    public static void main(final String[] args) {
        final FootInch fi1 = new FootInch(5, 9);
        final FootInch fi2 = new FootInch(9, 11);

        System.out.println(fi1.add(fi2)); // App.FootInch(feet=15, inches=8)
    }

    @Data
    public static class FootInch {

        private final int feet;
        private final int inches;

        public FootInch add(/* @Nonnull */final FootInch fi2)
        {
            // first we sum directly
            int newFeet = this.feet + fi2.getFeet();
            int newInches = this.inches + fi2.getInches();
            // then we "transfert" every 12 inches to 1 foot:
            newFeet += newInches / 12;
            newInches = newInches % 12;
            // return a new FootInch for the result
            return new FootInch(newFeet, newInches);
        }
    }
}

demo here

答案 5 :(得分:0)

好吧,如果你考虑5.9英尺,它实际上不是5英尺9英寸。相反,它是5英尺,10.8英寸。这是因为:

  

1英尺= 12英寸

     

5.9英尺= 70.8英寸

     

70.8英寸= 5英尺7.5英寸

所以,我认为不应该使用float来表示你的值,因为它不会非常准确。

我认为您应该尝试以下方法之一:

  1. 使用字符串表示长度。然后实现合适的提取算法。
  2. 使用两个不同的变量来存储英尺和英寸。
  3. 正确获取值后,将两者都转换为英寸,添加,然后再将两部分分开。

答案 6 :(得分:0)

我建议为此创建一个类,但这个草率的代码也可以运行:

float a = 5.9f;
float b = 9.11f;

double f1 = Math.floor(a); // feet of a
double f2 = Math.floor(b); // feet of b

double feet = f1 + f2; // combined feet

int in1 = 0, in2 = 0; // inches of a and b

String s1 = String.valueOf(a), s2 = String.valueOf(b); // String values of a and b

if (s1.contains(".")) // if a contains a radix point
{
    // inches of a is number after radix point
    in1 = Integer.parseInt(s1.substring(s1.indexOf(".") + 1));
}

if (s2.contains(".")) // if b contains a radix point
{
    // inches of b is number after radix point
    in2 = Integer.parseInt(s2.substring(s2.indexOf(".") + 1));
}

int in = in1 + in2; // combined extra inches

feet += in / 12; // add a foot for every 12 extra inches

// take all of the factors of 12 out of the total extra inches (in % 12)
System.out.print((int)feet + " feet " + in % 12 + " inches");

这会生成输出:

15 feet 8 inches