I'm not sure what to do next in this program. Basically, I'm not sure exactly how to get the time to output accurately, as in what forumla I should be using.
This is the question:
What comes 13 hours after 4 o'clock? Create an ElaspedTimeCalculator application that prompts the user for a starting hour, whether it is am or pm, and the number of elapsed hours. The application then displays the time after that many hours have passed. Application output should look similar to:
Enter the starting hour: 7
Enter am or pm: pm
Enter the number of elapsed hours: 10
The time is: 5:00 am
Here's the code I have so far:
import java.util.Scanner;
public class ElapsedTimeCalculator
{
public static void main(String[] args)
{
int starting_hour;
int starting_minutes; /*This is added in case the user wants to add minutes as well.*/
String am_or_pm;
int elapsed_hours;
int elasped_minutes;
int time_hours;
int time_minutes;
System.out.println("Welcome. This application will give you the time based on your input.");
System.out.println(" ");
Scanner input = new Scanner(System.in);
System.out.print("Enter the starting hour: ");
starting_hour = input.nextDouble();
System.out.print("Enter the starting minutes: ");
starting_minutes = input.nextDouble();
System.out.print("Enter either 'am' or pm': ");
am_or_pm = input.nextString();
System.out.print("Enter the number of elapsed hours: ");
elapsed_hours = input.nextDouble();
input.close();
time_hours =
time_minutes =
if(am_or_pm = "am" || am_or_pm = "a.m." || am_or_pm = "AM" || am_or_pm = "A.M.")
{
System.out.println("The time is " + time_hours + ":" + time_minutes + "am");
}
if(am_or_pm = "pm" || am_or_pm = "p.m." || am_or_pm = "PM" || am_or_pm = "P.M.")
{
System.out.println("The time is " + time_hours + ":" + time_minutes + "pm");
}
}
}
To calculate
time_hours
should I just calculate this by adding the elapsed hour to the starting hour? I doubt it will be accurate for all situations.
Same for the
time_minutes
For example, if the starting minutes and the elapsed minutes were 50, it would be greater than 60. Also, not sure if it makes sense to separate hours and minutes like this, it's not required to in the question. I initally thought it would be easier to approach like this instead of allowing the user to input a double for the starting hour. ex. 5.7
I get the feeling that this is extremely simple, but nonetheless, I'm stuck, so any help would be appreciated.