Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.Scanner;
- public class TravellingAtLightSpeed {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- BigDecimal lightYears = BigDecimal.valueOf(Double.parseDouble(console.nextLine()));
- BigDecimal kmPerLightYear = BigDecimal.valueOf(9450000000000.0);
- BigDecimal distanceInKm = BigDecimal.ZERO;
- distanceInKm = lightYears.multiply(kmPerLightYear);
- BigDecimal speed = BigDecimal.valueOf(300000.0);
- BigDecimal totalSeconds = distanceInKm.divide(speed, 100, RoundingMode.HALF_EVEN);
- BigDecimal totalWeeks = totalSeconds.divide(new BigDecimal("604800"), 0, RoundingMode.FLOOR);
- totalSeconds = totalSeconds.subtract(totalWeeks.multiply(new BigDecimal("604800")));
- BigDecimal totalDays = totalSeconds.divide(new BigDecimal("86400"), 0, RoundingMode.FLOOR);
- totalSeconds = totalSeconds.subtract(totalDays.multiply(new BigDecimal("86400")));
- BigDecimal totalHours = totalSeconds.divide(new BigDecimal("3600"), 0, RoundingMode.FLOOR);
- totalSeconds = totalSeconds.subtract(totalHours.multiply(new BigDecimal("3600")));
- BigDecimal totalMinutes = totalSeconds.divide(new BigDecimal("60"), 0, RoundingMode.FLOOR);
- totalSeconds = totalSeconds.subtract(totalMinutes.multiply(new BigDecimal("60")));
- totalSeconds = totalSeconds.setScale(0, RoundingMode.HALF_EVEN);
- System.out.println(totalWeeks + " weeks");
- System.out.println(totalDays + " days");
- System.out.println(totalHours + " hours");
- System.out.println(totalMinutes + " minutes");
- System.out.println(totalSeconds + " seconds");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement