Java 添加两个以英尺和英寸为单位的高度测量值

Java 添加两个以英尺和英寸为单位的高度测量值,java,Java,我有两个浮点值输入5.9f,9.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);

我有两个浮点值输入
5.9f
9.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英寸时,我无法获得正确的输出。

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"

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"

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"

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"

试试这样的

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

试试这样的

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

试试这样的

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

试试这样的

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
尝试以下方法(使用专门处理数字的JDK类):

尝试以下方法(使用专门处理数字的JDK类):

尝试以下方法(使用专门处理数字的JDK类):

尝试以下方法(使用专门处理数字的JDK类):


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

但是,Math.round将舍入到最接近的十进制值,因此:

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

但是,这会给您带来一个问题:0.1=0.10,那么这是1英寸还是10英寸?我建议你用0.01表示0英尺1英寸。那么你有:

e = (a%1)*100

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

但是,Math.round将舍入到最接近的十进制值,因此:

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

但是,这会给您带来一个问题:0.1=0.10,那么这是1英寸还是10英寸?我建议你用0.01表示0英尺1英寸。那么你有:

e = (a%1)*100

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

但是,Math.round将舍入到最接近的十进制值,因此:

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

但是,这会给您带来一个问题:0.1=0.10,那么这是1英寸还是10英寸?我建议你用0.01表示0英尺1英寸。那么你有:

e = (a%1)*100

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

但是,Math.round将舍入到最接近的十进制值,因此:

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

但是,这会给您带来一个问题:0.1=0.10,那么这是1英寸还是10英寸?我建议你用0.01表示0英尺1英寸。那么你有:

e = (a%1)*100
使用Lombok作为构造函数、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);
        }
    }
}

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);
        }
    }
}

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);
        }
    }
}

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);
        }
    }
}

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

1英尺=12英寸

5.9英尺=70.8英寸

70.8英寸=5英尺7.5英寸

所以,我认为你不应该用float来表示你的值,因为它不太准确

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

  • 使用字符串表示长度。然后实现合适的提取算法
  • 使用两个不同的变量来存储英尺和英寸

  • 在正确获取值后,将两者转换为英寸、加法,然后再将这两个部分分开。

    < P>,如果你考虑5.9英尺,实际上不是5英尺9英寸。而是5英尺10.8英寸。这是因为:

    1英尺=12英寸

    5.9英尺=70.8英寸

    70.8英寸=5英尺7.5英寸

    所以,我认为你不应该用float来表示你的值,因为它不太准确

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

  • 使用字符串表示长度。然后实现合适的提取算法
  • 使用两个不同的变量来存储英尺和英寸

  • 在正确获取值后,将两者转换为英寸、加法,然后再将这两个部分分开。

    < P>,如果你考虑5.9英尺,实际上不是5英尺9英寸。而是5英尺10.8英寸。这是因为:

    1英尺=12英寸

    5.9英尺=70.8英寸

    70.8英寸=5英尺7.5英寸

    所以,我认为你不应该用float来表示你的值,因为它不太准确

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

  • 使用字符串表示长度。然后实现合适的提取算法
  • 使用两个不同的变量来存储英尺和英寸

  • 在正确获取值后,将两者转换为英寸、加法,然后再将这两个部分分开。

    < P>,如果你考虑5.9英尺,实际上不是5英尺9英寸。而是5英尺10.8英寸。这是因为:

    1英尺=12英寸

    5.9英尺=70.8英寸

    70.8英寸=5英尺7.5英寸

    所以,我认为你不应该用float来表示你的值,因为它不太准确

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

  • 使用字符串表示长度。然后实现合适的提取算法
  • 使用两个不同的变量来存储英尺和英寸
  • 正确获取值后,将两者转换为英寸、相加和th