Here is my homework question:
Write a program that asks for a film name and the length of the film in minutes and then the program prints the name of the film and its length in hours and minutes, e.g. the user enters Finding Nemo and 104 then the program would output Finding Nemo runs for 1 hour and 44 minutes.
I have completed this question to the best of my ability as highlighted below, however I wondered if there may be a more efficient way of laying out the code or programming this to work. It works btw, just wondered if there is a better way to write the code?
import java.util.Scanner;
public class Film
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter Movie Name: ");
String movie = input.nextLine();
System.out.println("Enter Movie Length (minutes): ");
int totalmins = input.nextInt();
int hours = totalmins/60;
int minutes = totalmins%60;
if(hours==1) {System.out.print(movie + " runs for " + hours + " hour and ");}
else {System.out.print(movie + " runs for " + hours + " hours and ");}
if(minutes==1) {System.out.println(minutes + " minute ");}
else {System.out.println(minutes + " minutes ");}
}
}
I think your code is simple and good to get what you want. Your homework is complete. 🙂
However if you want more details on date and time you can try playing with java.util.Date and GregorianCalendar classes.