如何使用具有两个字符串值的数组

时间:2019-07-18 02:46:02

标签: c#

我正在编写一个程序,该程序将在标签中生成州资本,然后有一个带有州的组合框。我正在使用两个数组和一个随机数生成器来填写标签并选择一个状态。不确定如何使数组使用字符串并正确显示

//变量

    String[] capitalsString = new string[10];
    String[] stateString = new string[10];
    int numberInteger;
    Random numberRandom = new Random();

    public mainForm()
    {
        InitializeComponent();
    }

    private void answerButton_Click(object sender, EventArgs e)
    {
        // Checks for correct answer
        if (statesComboBox.Text.ToUpper() == stateString[numberInteger].ToUpper())
        {
            MessageBox.Show("Correct!", "Very Good!", MessageBoxButtons.OK);
        }
        else
        {
            MessageBox.Show("Sorry, the correct answer is: " + stateString[numberInteger], "Incorrect", MessageBoxButtons.OK);
            statesComboBox.Text = "";
            SelectNumber();
        }            
    }

    private void LoadArrays()
    {
        // Defines the two arrays and loads the capitals in the combo box

        int countInteger = 0;

        capitalsString[0] = "Little Rock";
        capitalsString[1] = "Springfield";
        capitalsString[2] = "Des Moines";
        capitalsString[3] = "Topeka";
        capitalsString[4] = "Baton Rouge";
        capitalsString[5] = "St. Paul";
        capitalsString[6] = "Jefferson City";
        capitalsString[7] = "Lincoln";
        capitalsString[8] = "Albany";
        capitalsString[9] = "Austin";

        stateString[0] = "Arkansas";
        stateString[1] = "Illinois";
        stateString[2] = "Iowa";
        stateString[3] = "Kansas";
        stateString[4] = "Louisianna";
        stateString[5] = "Minnesota";
        stateString[6] = "Missouri";
        stateString[7] = "Nebraska";
        stateString[8] = "New York";
        stateString[9] = "Texas";

        while (countInteger <= 10)
        {
            statesComboBox.Items.Add(stateString[countInteger]);
            countInteger += 1;
        }
    }

    private void mainForm_Load(object sender, EventArgs e)
    {
        // Loads the Arrays and Combo box
        // Selects a Capital

        LoadArrays();
        SelectNumber();

    }

    private void SelectNumber()
    {
        numberInteger = numberRandom.Next(0, 10);
        capitalsLabel.Text = capitalsString[numberInteger];

应该在标签框中显示一个大写字母,并且用户选择他们所处的字母大写状态。该代码无法正确运行,因为它会使组合框中的状态加倍,并且不向大写字母添加大写字母。标签盒。我不确定该在哪里进行。

0 个答案:

没有答案