dimanche 20 septembre 2015

Printing out the wrong operator, dice-game

I got a problem with a pretty simple Windows Forms program. When running the program, which is a dice-game, you write into a textbox how many dices you want to roll. The application should then roll the dices, depending of how many dices youve choosed to roll. And show the values all the dices get, and adding them together. So if i roll 5 dices, the application should write out the values, and the sum of all the values. Example for 4 dices: 4 + 3 + 6 + 1 = 14.

The problem ive got is that i dont know how to make the application decide if it rolled the dices enough times before writing an extra + sign out. This is what ive written so far.

        private void throwDice_Click(object sender, EventArgs e)
        {
        int numberOfDices = int.Parse(dicesTextBox.Text);
        int sum = 0;
        showLabel.Text = "";

        if (numberOfDices == 1)
        {
            Random rand = new Random();
            int dice1 = rand.Next(1, 7);
            sum = dice1;
            showLabel.Text = dice1 + " = " + sum;
        }
        else if (numberOfDices > 1)
        {
            Random rand1 = new Random();

            for (int i = 0; i <= numberOfDices; i++)
            {
                int dice = rand1.Next(1, 7);
                sum += dice;

                if (i != antal)
                {
                    visaLabel.Text += dice.ToString() + " + ";
                }
                else
                {
                    visaLabel.Text += "=" + summa.ToString();
                }
            }
        }
    }
}

}

So the application writes out for example with 3 dices: 3 + 3 + 3 += 9. The problem is the last + in the label. I want it to only write =9, not another +. Hope you understand what i mean and what im after.

Best Regards,

Marcus Persson




Aucun commentaire:

Enregistrer un commentaire