0

我真的很接近让它正常工作,但我只是错过了一些东西。这可能很简单。我对 C# 很陌生。我有一个名为的文本框txtState,您可以在其中输入一个城市(我知道),以查看您是否访问过这个城市。txtAnswer当您单击btnVisited按钮时,它会向另一个文本框输出响应。现在它只是将我输入的任何内容输入到txtState框中并说它是数组中的 0 位置。我忘了提到我需要在数组中包含这个城市的位置,并将其输出到文本框。这是我的代码:

namespace Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        txtAnswer.Text = "";
        txtState.Text = "";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnVisited_Click(object sender, EventArgs e)
    {
        string[] CityName = {"Columbus", "Bloomington", "Indianapolis",
        "Fort Wayne", "Greensburg", "Gary", "Chicago", "Atlanta", "Las Vegas"};
        bool visitedbool;
        int subscript;
        subscript = 0;
        visitedbool = false;
        string QueryCity;
        QueryCity = txtState.Text.ToUpper();
        do 
        {
            subscript += 0;
            if (subscript == 0)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "city you have visited.";

            else if (subscript == 1)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "st city you have visited.";

            else if (subscript == 2)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "nd city you have visited.";

            else if (subscript == 3)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "rd city you have visited.";

            else if (subscript == 4 - 8)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "th city you have visited.";

            else
                txtAnswer.Text = "You have not visited this city.";
        }
        while (visitedbool == true);       
    }
}
}
4

1 回答 1

1

您的代码的第一个问题:

这不起作用:else if (subscript == 4 - 8)

试试这个:else if (subscript >= 4 && subscript <= 8)

第二:

subscript += 0;是相同的subscript = subscript + 0;

如果您将下标用作某种计数器,则它将不起作用,因为您只是在添加0

第三:

visitedbool永远都是false。您的循环只会运行一次,因为您从未设置visitedbooltrue

于 2012-11-24T17:18:22.133 回答