为什么我不能添加由我的方法创建的两个随机值

时间:2014-10-14 03:34:59

标签: c# visual-studio

所以我试图添加我的函数返回的两个值,但它不断抛出错误。 我不明白为什么不添加它们? 我需要能够将这两个值加在一起,这样我才能得到这个总和 并计算掷骰子的总和是否为中奖号码。

namespace CS_DiceGame_
{
    public partial class diceThrowMainWindow : Form
    {
        // This is a dice throw game, that rolls a random dice number
        // then tells the user whether they won or loss
        // written by Dakota Wisdom Lorance
        // Oct. 13, 2014

        public diceThrowMainWindow()
        {
            InitializeComponent();
        }

        private int firstDieRoll ()
        {
            int firstDieValue;
            Random rand1 = new Random();
            firstDieValue = rand1.Next(6) + 1;

            switch (firstDieValue)
            {
                case 1:
                    firstDiePictureBoxOne.Visible = true;
                    firstDiePictureBoxTwo.Visible = false;
                    firstDiePictureBoxThree.Visible = false;
                    firstDiePictureBoxFour.Visible = false;
                    firstDiePictureBoxFive.Visible = false;
                    firstDiePictureBoxSix.Visible = false;
                    break;

                case 2:
                    firstDiePictureBoxOne.Visible = false;
                    firstDiePictureBoxTwo.Visible = true;
                    firstDiePictureBoxThree.Visible = false;
                    firstDiePictureBoxFour.Visible = false;
                    firstDiePictureBoxFive.Visible = false;
                    firstDiePictureBoxSix.Visible = false;
                    break;

                case 3:
                    firstDiePictureBoxOne.Visible = false;
                    firstDiePictureBoxTwo.Visible = false;
                    firstDiePictureBoxThree.Visible = true;
                    firstDiePictureBoxFour.Visible = false;
                    firstDiePictureBoxFive.Visible = false;
                    firstDiePictureBoxSix.Visible = false;
                    break;

                case 4:
                    firstDiePictureBoxOne.Visible = false;
                    firstDiePictureBoxTwo.Visible = false;
                    firstDiePictureBoxThree.Visible = false;
                    firstDiePictureBoxFour.Visible = true;
                    firstDiePictureBoxFive.Visible = false;
                    firstDiePictureBoxSix.Visible = false;
                    break;

                case 5:
                    firstDiePictureBoxOne.Visible = false;
                    firstDiePictureBoxTwo.Visible = false;
                    firstDiePictureBoxThree.Visible = false;
                    firstDiePictureBoxFour.Visible = false;
                    firstDiePictureBoxFive.Visible = true;
                    firstDiePictureBoxSix.Visible = false;
                    break;

                case 6:
                    firstDiePictureBoxOne.Visible = false;
                    firstDiePictureBoxTwo.Visible = false;
                    firstDiePictureBoxThree.Visible = false;
                    firstDiePictureBoxFour.Visible = false;
                    firstDiePictureBoxFive.Visible = false;
                    firstDiePictureBoxSix.Visible = true;
                    break;
            }

            return firstDieValue;
        }

        private int secondDieRoll ()
        {
            int secondDieValue;
            Random rand2 = new Random();
            secondDieValue = rand2.Next(6) + 1;

            switch (secondDieValue)
            {
                case 1:
                    secondDiePictureBoxOne.Visible = true;
                    secondDiePictureBoxTwo.Visible = false;
                    secondDiePictureBoxThree.Visible = false;
                    secondDiePictureBoxFour.Visible = false;
                    secondDiePictureBoxFive.Visible = false;
                    secondDiePictureBoxSix.Visible = false;
                    break;

                case 2:
                    secondDiePictureBoxOne.Visible = false;
                    secondDiePictureBoxTwo.Visible = true;
                    secondDiePictureBoxThree.Visible = false;
                    secondDiePictureBoxFour.Visible = false;
                    secondDiePictureBoxFive.Visible = false;
                    secondDiePictureBoxSix.Visible = false;

                    break;

                case 3:
                    secondDiePictureBoxOne.Visible = false;
                    secondDiePictureBoxTwo.Visible = false;
                    secondDiePictureBoxThree.Visible = true;
                    secondDiePictureBoxFour.Visible = false;
                    secondDiePictureBoxFive.Visible = false;
                    secondDiePictureBoxSix.Visible = false;

                    break;

                case 4:
                    secondDiePictureBoxOne.Visible = false;
                    secondDiePictureBoxTwo.Visible = false;
                    secondDiePictureBoxThree.Visible = false;
                    secondDiePictureBoxFour.Visible = true;
                    secondDiePictureBoxFive.Visible = false;
                    secondDiePictureBoxSix.Visible = false;

                    break;

                case 5: 
                    secondDiePictureBoxOne.Visible = false;
                    secondDiePictureBoxTwo.Visible = false;
                    secondDiePictureBoxThree.Visible = false;
                    secondDiePictureBoxFour.Visible = false;
                    secondDiePictureBoxFive.Visible = true;
                    secondDiePictureBoxSix.Visible = false;
                    break;

                case 6:
                    secondDiePictureBoxOne.Visible = false;
                    secondDiePictureBoxTwo.Visible = false;
                    secondDiePictureBoxThree.Visible = false;
                    secondDiePictureBoxFour.Visible = false;
                    secondDiePictureBoxFive.Visible = false;
                    secondDiePictureBoxSix.Visible = true;
                    break;
            }
            return secondDieValue;
        }

        private void currentRollValue ()
        {
            secondDieRollValue + firstDieRollValue;
        }

        private void throwDiceButton_Click(object sender, EventArgs e)
        {
            firstDieRoll();
            secondDieRoll();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你有一个随机的计算..没有在这里分配任何东西:

private void currentRollValue ()
{
    secondDieRollValue + firstDieRollValue; // <-- this does nothing.
}

首先,您需要将变量移动到类级别,以便它们可以在方法之间共享:

public partial class diceThrowMainWindow : Form
{
    int firstDieValue = 0;
    int secondDieValue = 0;

    // .. the rest here

然后你需要将它分配给某个东西,然后在某个地方显示它:

var result = secondDieValue + firstDieValue; 
someLabel.Text = result.ToString();