Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have made a simple number generator, and I have a question: is it possible for the generator to eject "red", "blue", "green", " yellow" and "white" instead of the numbers 1-5?

namespace zufallsgenerator
{
    public partial class Form1 : Form
    {
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnWhlie_Click(object sender, EventArgs e)
        {
            int summe = 0, z; 

            lblAnzeige.Text = " ";

           while (summe <= 0)
           {
                z = r.Next(1, 6);
                summe = summe + z;
           }
           lblAnzeige.Text += summe + "\n";               
        }
    }
}
share|improve this question
2  
I feel like this question would be more appropriate for StackOverflow, as this site is mainly for reviewing code –  Robert Snyder Oct 22 '13 at 14:27
add comment

closed as off-topic by Jesse C. Slicer, Malachi, svick, palacsint, Jamal Oct 22 '13 at 15:41

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – svick, palacsint, Jamal
  • "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – Jesse C. Slicer, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 2 down vote accepted

Why don't you just make an array for lookup?

string[] colors = {"red", "blue", "green","yellow","white"};

then you simply map your random number to a color string by using it as the index of the array (-1 as array indices start with 0):

lblAnzeige.Text += colors[summe-1] + "\n";
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.