如何将米转换为英里,并以英尺和英寸为单位给出余数?

时间:2016-01-27 18:05:36

标签: java

不知何故输出最终完全错误。有什么方法可以解决这个问题吗?

import java.util.Scanner;

public class MeterConversion {

    public static void main(String[] args) {


        int meters;
        System.out.print("Enter meters: ");
        Scanner userInput = new Scanner (System.in);
        meters = userInput.nextInt();

        double inches = (3.3 * 12 * meters);
        int feet = (int) (inches / 12);
        int miles = (feet / 5280);
        int milesConversion = (miles - (meters % miles));
        int feetConversion = ((miles - milesConversion) % feet);
        int inchesConversion = (int) (feetConversion % inches);

        System.out.print(meters + " meter(s) converts to " 
        + milesConversion +" mile(s), " + feetConversion + " feet, " 
        + inchesConversion + " inch(es)");

    }

}

1 个答案:

答案 0 :(得分:2)

试试这个:

import java.util.Scanner;

public class MeterConversion {

    public static void main(String[] args) {

        int meters;
        System.out.print("Enter meters: ");
        Scanner userInput = new Scanner (System.in);
        meters = userInput.nextInt();

        double inches = (39.370078 * meters);
        int miles = (int) (inches / 63360);
        int feet =  (int) (inches - miles * 63360) / 12;
        double inchesRemaining = inches - (miles*63360 + feet*12);

        System.out.print(meters + " meter(s) converts to " 
        + miles +" mile(s), " + feet + " feet, " 
        + inchesRemaining + " inch(es)");
    }
}

我是这样编写的,因此从一个单元到另一个单元的转换更容易理解。希望它有所帮助。