将英寸转换为英里,英尺和英寸

时间:2014-02-01 20:40:11

标签: java

我必须将英寸换成英里。然后在计算里程后计算剩余的英尺。然后计算之后的剩余英寸。我得到的输出是19英里,19英尺,7英寸,它应该是19英里,2560英尺,7英寸。

import java.util.Scanner;

/**
 * @author abc
 * @version 1-28-2014
 */

public class DistanceConverter   {


   public static void main(String[] args) {

      Scanner userInput = new Scanner(System.in);

      int numOfInches;

      System.out.print("Enter the raw measurement in inches: ");
      numOfInches = userInput.nextInt();

      if (numOfInches < 0)   {
         System.out.println("Measurement must be non-negative!");
      }

      int feet = numOfInches / 12;
      int miles = feet / 5280;
      int remainingFeet = miles % feet;
      int remainingInches = numOfInches % 12;

      if (numOfInches > 0)   {
         System.out.println("\n\nMeasurement by combined miles, feet, inches:");
         System.out.println("\tmiles: " + miles);
         System.out.println("\tfeet: " + remainingFeet);
         System.out.println("\tinches " + remainingInches);
         System.out.println("\n\n" + numOfInches + " inches = " + miles 
            + " miles, " + remainingFeet + " feet, " + remainingInches + " inches");
      }
   }
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

 if( numOfInches > 0 ) {
   int inchesInFeet = 12;
   int feetsInMile = 5280;
   int inchesInMile = feetsInMile * inchesInFeet;
   int miles = numOfInches / inchesInMile;
   int remainingInches = numOfInches % inchesInMile;
   int feet = remainingInches / inchesInFeet;
   remainingInches = remainingInches % inchesInFeet;

   System.out.println( "\n\nMeasurement by combined miles, feet, inches:" );
   System.out.println( "\tmiles: " + miles );
   System.out.println( "\tfeet: " + feet );
   System.out.println( "\tinches " + remainingInches );
   System.out.println( "\n\n" + numOfInches + " inches = " + miles 
     + " miles, " + feet + " feet, " + remainingInches + " inches" );
 }