java - How can I convert from meters to miles, and give the remainder in feet and inches? -
this question has answer here:
- why result of 1/3 == 0? 12 answers
somehow output keeps ending wrong. way fix this?
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 " + milesconversion +" mile(s), " + feetconversion + " feet, " + inchesconversion + " inch(es)"); } }
try this:
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 " + miles +" mile(s), " + feet + " feet, " + inchesremaining + " inch(es)"); } }
i wrote way easier understand in terms of conversion 1 unit another. hope helps.
Comments
Post a Comment