• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

having issue with a loops problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was told to create a program that prints the time between 00:00 (0:00 a.m.) and 23:45 (11:45 p.m.) in the 24-hour clock and 12-hour clock format like this:
24-hour Clock 12-hour Clock
-----------------------------
00:00 0:00 a.m.
00:15 0:15 a.m.
00:30 0:30 a.m.
00:45 0:45 a.m.
01:00 1:00 a.m.
01:15 1:15 a.m.
01:30 1:30 a.m.
01:45 1:45 a.m.
02:00 2:00 a.m.


ect...

but cant seem to create the program and my program doesnt seem to run.
Help would be appreciated please and thankyou.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you have so far, and where are you stuck making progress?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

As Ulf said, please show us what you achieved already.
 
harman brar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

As Ulf said, please show us what you achieved already.



SOrry for late reply i just saw my email, thank you guys for the warm welcome, my code is below it just seems to loop the wrong way over and over again... please inform me on what im doing wrong. Thank you!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are doing wrong is writing too much code at a time. just try and do one little part at a time. for example, see if you can just print the hours, from 0 to 24.

Once you can do that, see if you can print the hours twice, once in 24-hour format, and once in 12 hour format.

Then try and add in the next piece, and then the next.

Never try and do part 2 until you have part 1 working.
 
harman brar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:What you are doing wrong is writing too much code at a time. just try and do one little part at a time. for example, see if you can just print the hours, from 0 to 24.

Once you can do that, see if you can print the hours twice, once in 24-hour format, and once in 12 hour format.

Then try and add in the next piece, and then the next.

Never try and do part 2 until you have part 1 working.



Thannks for the advice, i've been trying to create it but still unable to come up with the right program. im unable to keep the minuites and hour together and change at the right times from one hour to the next...
 
harman brar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:What you are doing wrong is writing too much code at a time. just try and do one little part at a time. for example, see if you can just print the hours, from 0 to 24.

Once you can do that, see if you can print the hours twice, once in 24-hour format, and once in 12 hour format.

Then try and add in the next piece, and then the next.

Never try and do part 2 until you have part 1 working.


This is what i;ve gotten so far
'/**
* Auto Generated Java Class.
*/
public class Time {


public static void main(String[] args) {
String heading1 = "24-hour Clock ";
String heading2 = "12-hour Clock";

System.out.printf( "%-15s %15s %n", heading1, heading2);
System.out.println("-----------------------------");

for (int j = 0; j <= 45; j+=15){ //Minutes
for (int i = 0; i <= 12; i++){ //12-Hours
for (int y = 0; y <= 45; y+=15){ //Minutes
for (int x = 00; x <= 24; x++){ //24-Hours
}
System.out.println( i + ":" + j, x + ":" y);
}
}

/* ADD YOUR CODE HERE */
}
}
}


any tips on how i can fix it it seems to loop the hours 1-12 then loop the mins after a cycle.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

harman brar wrote:Thannks for the advice


Assuming you followed that advice, why don't you show us the last piece of code where it was actually printing what you wanted.
i.e. if you got as far as Fred's suggestion of

see if you can just print the hours, from 0 to 24.

then show us that code

if you got as far as

see if you can print the hours twice, once in 24-hour format, and once in 12 hour format.

show us that code, etc
 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I spent far too much time dinking around with this this morning.

First suggestion: turn off the computer, find a pad and pencil, layout the process on paper using said pencil, step by step until you have the design complete. Only then should you turn on Mr. Computer and begin coding
Secondly, we really do need to see your code. I'm surprised no one mentioned the we are not a code shop yet, but we are not a code shop. Code with a couple of loops and an all caps your code goes here is not going to garner you much help. It implies you want someone to write the project for you, and not a plea for help.
Third, codeblocks. Look at the first line in the format options while in message creation -- see the button marked [code] ? Press that, paste your code between the code tags.
Finally, you can reduce your logic to two loops, instead of four. You know you need an outer loop that counts to 24 (actually it counts from Zero to 23). Your second loop needs only calculate the quarter hour, which is 15 minute increments. The rest is string formatting.

Regards,
Robert
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree.

Why on earth are you working somewhere where you are given automatically generated code to complete. I added code tags, which usually make the code look a lot better, but unfortunately in this instance they highlight the inconsistent indentation.
I think you need to delete all that code and start from scratch, with your computer safely turned off. Also you write a few lines at a time, compile and run them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic