1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# Newbie - two weeks, help please

Discussion in 'C#' started by hwytt3, Oct 10, 2010.

  1. hwytt3

    hwytt3 New Member

    Joined:
    Oct 10, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello fellow programmers. I've started a new career path since my plant has shut down. C# programming and I am needing help. My class assignment was to make a console program that will display all possibilities of Military Time. I need the loop to stop the second time around at the 3 on the hours. I goes to 29 : 59 : 58 and below is what I have now.


    Code:
    static void Main(string[] args)
    		{
    			for(int h = 0; h <= 2; h++)
    			{
    				for(int H = 0; H <= 9; H++)
    				{
    					for(int m = 0; m <= 5; m++)
    					{
    						for(int M = 0; M <= 9; M++)
    						{
    							for(int s = 0; s <= 5; s++)
    							{
    								for(int S = 0; S <= 9; S++)
    								{
    								Console.WriteLine(h.ToString() + H.ToString() + " : " + m.ToString() 
    												+ M.ToString() + " : " + s.ToString() + S.ToString());
    								}
    							}
    						}
    					}
    				}
    			}
    		string junk = Console.ReadLine();	
    		}
    
    Can anyone help tell me what I have to do to get the loop to stop on the 23rd. hour. If I use a 3 in the H the time jumps from 3 hour to 10 hours instead of going through but I'm not sure how to stop the loop on just that one.

    Thank you for any help.
     
  2. Love.NET

    Love.NET New Member

    Joined:
    Nov 20, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Maybe you want your result at stop point to be "23:00:00"?
    It's so easy to make a loop stop.
    To stop a loop you can use "break;" with some if condition.
    for an example without using "break", you can put the following line in your last For loop with S (the most inner loop and goes after the output command "Console.WriteLine"):
    if(h==2&&H==3) M=m=H=h=S=s=60;
    you can use another number greater than 60 that is mainly to make all conditions in all For loops to be not agreed.
    If you want to use "break", you have to put it in every For loop and use a proper if condition in each one. (like the form "if something break;" to exit the for loops one by one from inside to outside).
    Hope that help !
    Thanks.
     
  3. Love.NET

    Love.NET New Member

    Joined:
    Nov 20, 2010
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    I'm sorry, The least number you can use instead of 60 above is 10.
    Thanks !
     

Share This Page