C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help. I need really need help. Posted by Opticknerve on 18 May 2009 at 5:48 AM
How do i make the elements of an array to print in reverse order

using System;
enum Colors {Red, Blue, Yellow, Purple, Green, Black, White, Orange, Pink, Gold}
class Colours
{
static void Main()
{
Console.WriteLine("Please choose your three most favourite colors");
string [] colors = new string [10];
colors = Enum.GetNames(typeof(Colors));
for (int i = 0; i < colors.Length; i++) {
Console.WriteLine( colors[i]);
}

Console.WriteLine("Please choose your first color");
string color1= Console.ReadLine();
Console.WriteLine("Please choose your second color");
string color2 = Console.ReadLine();
Console.WriteLine("Please choose your third color");
string color3 = Console.ReadLine();
Console.WriteLine("Here are your colors printed in reverse order {0}, {1}, {2}", color1, color2, color3);
}
}



How do i make the elements of an array to print in reverse order
Report
Re: Help. I need really need help. Posted by drewdb on 18 May 2009 at 8:44 PM
Is this the sort of thing you are after?

Console.WriteLine("Please choose your three most favourite colors");
string [] colors = new string [10];
colors = Enum.GetNames(typeof(Colors));
for (int i = colors.Length; i > -1; i--) { **********************
Console.WriteLine( colors[i]);

iterating backwards through your array.
Report
Re: Help. I need really need help. Posted by Psightoplazm on 18 May 2009 at 8:47 PM
you'll also want to start with colors.Length-1, otherwise you will get an out of bounds exception.
></\/~Psightoplasm`~



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.