The problem
Write a program that displays the conversion from miles to kilometers.
Breaking it down
Creating a range of number from 1 to 10 calling the Stream.forEach
will print the table and displaying the calculation for converting miles to kilometers.
public static void main(String[] args) {
System.out.printf("%-12s%-8s\n", "Miles", "Kilometers");
IntStream.rangeClosed(1, 10).forEach(i -> {
System.out.printf("%-12d%-8.3f\n", i, i * 1.609);
});
}
Output
Miles Kilometers
1 1.609
2 3.218
3 4.827
4 6.436
5 8.045
6 9.654
7 11.263
8 12.872
9 14.481
10 16.090