Ryan

Coding Help

6 posts in this topic

So I wasn't really sure if I should post this here since it isn't GSC coding but I'm hoping someone here can help me with this Java coding exercise for school. So here's the problem, I'm trying to make a Time Calculator where you ask the user for a certain time like 7 then ask if it's am or pm then ask them to enter an elapsed amount of time like 10 so the output will be 5:00am. I got everything to work but whenever the user enters a number like 18 as the elapsed amount instead of 10, I get 13:00 which is not what I want since it's supposed to be a 12 hour clock. So now I need to make it into a loop so I can enter any amount of elapsed time and get the correct answer and this is where I'm stuck. I don't really know where to begin with it. I'll paste my code that I have so far down below. Thanks. If you find my explanation confusing, I'll try to reword it the best I can.

package chapter5ex11;
import java.util.Scanner;
public class Chapter5Ex11 
{
    public static void main(String[] args) 
    {
        //Variables
        int usernum, elapsed, hour,i=0;
        String AmPm;
        Scanner input = new Scanner(System.in);
        
        //Input
        System.out.format("%13s %1s"," ","Enter the starting hour: ");
        usernum = input.nextInt();
        System.out.format("%13s %1s"," ","Enter am or pm: ");
        AmPm = input.next();
        System.out.format("%13s %1s"," ","Enter the number of elapsed hours: ");
        elapsed = input.nextInt();
        input.close();
        
        //Output 
        if(elapsed>=12-usernum)
        {
            if("am".equals(AmPm))
            {
                AmPm="pm";
            }
            else
            {
                AmPm="am";
            }
        }
        if(usernum<=12)
        {
            hour=usernum+elapsed;
            if(hour>12)
            {
                i=hour-12;
                System.out.format("%13s %1s\n"," ","The time "
                        + "is: "+i+":00"+AmPm);
            }
            else
            {
                System.out.format("%13s %1s\n"," ","The time "
                        + "is: "+hour+":00"+AmPm);
            }
        }
    }
}
0

Share this post


Link to post
Share on other sites

@Cat is the one you should ask tbh. He knows Java from school aswell.

0

Share this post


Link to post
Share on other sites

I'm not quite understanding your issue here.

 

If I understand correctly, your goal is to:

 

1) Ask a user for the input of the current time(In hours)

2) Select AM or PM

3) Ask a user for the input of the time elapsed(In hours)

4) Your code to print out the end result after x amount of hours has transpired since your original start time.

 

Also, your code needs to have a loop so that it restarts every time upon completion? If so, then it'll be an endless loop of just asking for inputs, and you'd have to add something in which exits the script of you don't enter anything, which is possible in a simple if statement and adding exit(); in the else section. I'm headed off to bed now, hopefully you can tell me if I'm right or not, and I'll make a few edits to your code if I have it right. GOod luck to you, let me know if I'm right! ^^

0

Share this post


Link to post
Share on other sites

You are right and I do know how to allow the user to end the program if it's on a loop, but what I need to do is make it so the user can enter higher amounts of numbers in the "Enter the number of elapsed hours" part of the code. So if I entered 7 as the starting hour, then PM, then for the elapsed hour I entered 18. The output would be wrong and would be 13:00 not 1:00 which Is what I want. The way I have my code setup is so that when the starting hour and elapsed hour are entered they are added together then 12 is subtracted to get the answer. But I need to make a loop so if it were above 24 as the sum of the starting hour and elapsed time it would subtract 24 instead. But I can't figure out what my parameters would be in the while loop.

0

Share this post


Link to post
Share on other sites

You are right and I do know how to allow the user to end the program if it's on a loop, but what I need to do is make it so the user can enter higher amounts of numbers in the "Enter the number of elapsed hours" part of the code. So if I entered 7 as the starting hour, then PM, then for the elapsed hour I entered 18. The output would be wrong and would be 13:00 not 1:00 which Is what I want. The way I have my code setup is so that when the starting hour and elapsed hour are entered they are added together then 12 is subtracted to get the answer. But I need to make a loop so if it were above 24 as the sum of the starting hour and elapsed time it would subtract 24 instead. But I can't figure out what my parameters would be in the while loop.

Here's what you should do. If you still struggle with it, I'll write some of the code for you, but since it's for a class I won't write it completely so as to help you learn so you can do it yourself next time. :)

 

1) Ask a user for an input. 

2) Ask for AM or PM.

3) Add an if statement, saying that if the input is PM, to add 12 to the number, so as to convert it into a 24 hour system. (e.g. An input of "7" and "PM" would add "12" to "7" to give you "19", which is what they're saying.

4) Take this new variable number, and add it to the elapsed time variable. That'll give you a new variable at a new time. (e.g. "19" plus "9" would give a result of "28", your new time.)

5) Add a for loop, saying if your new variable is greater than 24, to subtract 24 over and over until the number is less than 24. (e.g., "28" is greater than 24, so it would subtract 24, leaving you with "4", which is the time 9 hours after your starting time of 7 PM.)

6) Add another if statement, stating if the number is less than 12, to print the number and add the AmPm variable, and if the number is greater than or equal to 12, subtract 12 from it to get the number in 12-hour format instead of 24, and then add the AmPm variable so as to display Pm.

 

I hope this all made sense, I can add a little bit of a shell for you to follow if this is confusing, but hopefully it makes enough sense for you to follow.

0

Share this post


Link to post
Share on other sites

Here's what you should do. If you still struggle with it, I'll write some of the code for you, but since it's for a class I won't write it completely so as to help you learn so you can do it yourself next time. :)

 

1) Ask a user for an input. 

2) Ask for AM or PM.

3) Add an if statement, saying that if the input is PM, to add 12 to the number, so as to convert it into a 24 hour system. (e.g. An input of "7" and "PM" would add "12" to "7" to give you "19", which is what they're saying.

4) Take this new variable number, and add it to the elapsed time variable. That'll give you a new variable at a new time. (e.g. "19" plus "9" would give a result of "28", your new time.)

5) Add a for loop, saying if your new variable is greater than 24, to subtract 24 over and over until the number is less than 24. (e.g., "28" is greater than 24, so it would subtract 24, leaving you with "4", which is the time 9 hours after your starting time of 7 PM.)

6) Add another if statement, stating if the number is less than 12, to print the number and add the AmPm variable, and if the number is greater than or equal to 12, subtract 12 from it to get the number in 12-hour format instead of 24, and then add the AmPm variable so as to display Pm.

 

I hope this all made sense, I can add a little bit of a shell for you to follow if this is confusing, but hopefully it makes enough sense for you to follow.

 

Thanks! Was a little bit confusing but I think I'll be able to figure it out, though I'll have to wait till monday at school before doing anything. 

0

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now