Search code examples
javatimeintfinal

Java - Flight Schedule [university's homework]


I'm supposed to program a flight schedule and ran into a little problem.

The user has to insert: Departure Day (1 - 7, when 1 means the first day of the week and 7 the last day of the week). Departure Hour (0 - 24). Departure Minute (0 - 60).

Then : Flight's length in hours. Flight's length in minutes.

Then : The user gets the arrival time according to the data he/she inserted above.

For some reason, if I enter :

Departure time : Day 1, Hour 10, Minute 00.

Flight's length in hours 72 hours. Flight's length in minutes 00 minutes.

What I'm supposed to get is : Day 4, Hour 10, Minute 00. What I get is : Day 1, Hour 49, Minute 10.

-- EDIT -- Here's the new code:

import java.util.Scanner;
public class Flight
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter flight day: ");
        int FLYDAY = scan.nextInt();

        System.out.println("Enter flight hour: ");
        int FLYHOUR = scan.nextInt();

        System.out.println("Enter flight minute: ");
        int FLYMINUTE = scan.nextInt();

        int departureDay = scan.nextInt();
        int departureHour = scan.nextInt();
        int departureMin = scan.nextInt();

        departureMin += FLYMINUTE;
        while(departureMin >= 60) {
            departureHour++;
            departureMin-=60;
        }

        departureHour += FLYHOUR;
        while(departureHour >= 24)
        {
            departureDay++;
            departureHour-=24;
        }

        while(departureDay >=8)
        departureDay-=7;

        System.out.println("the supposed arrival time is: day- " + departureDay + ", hour- " + departureHour + ", minute- " + departureMin);
    }
}

Solution

  • You should change your if-statements to while-statements. For example, write

    while(departureMin >= 60) {
        departureHour++;
        departureMin-=60;
    }
    

    Additionally, your user input section is very wonky. A better way would be:

    Scanner scan = new Scanner (System.in);
    
    System.out.println("Enter flight day: ");
    int FLYDAY = scan.nextInt();
    
    System.out.println("Enter flight hour: ");
    int FLYHOUR = scan.nextInt();
    
    System.out.println("Enter flight minute: ");
    int FLYMINUTE = scan.nextInt();
    

    Follow the same format for asking how long the flight should last.