0

こんにちは、c# は初めてなので、私のコードがいたるところにあることをお許しください。

私のアプリケーションでは、カウンターを使用して if ステートメントを数えました。タイムピッカーから選択した変数 (日付) と呼ばれるテキスト ファイルを見つけたいと思います。そのテキスト ファイルの行数をカウントし、その数をカウント変数に追加して、13 未満かどうかを教えてほしいと思っています。

    private void button1_Click(object sender, EventArgs e)
    {
           int TotalLines(string date)
    {
        using (StreamReader r = new StreamReader(date))
        {
            int i = 0;
            while (r.ReadLine() != null) { i++; }
            return i;
        }
    }

    if
{
    i + counter = <13
}

    MessageBox.Show ("Seats Available");

    else 

    MessageBox.Show ("please chose another date");
    }
 }

}

ボタンのすべてのコードがかなり長いので、そのコードのセグメントを投稿したところです。

ご協力いただきありがとうございます

4

3 に答える 3

4

このようなことを意味していましたか?

private void button1_Click(object sender, EventArgs e)
{
    int counter = TotalLines(date);

    if (counter <= 13)
    {
        MessageBox.Show("Seats Available");
    }
    else
    {
        MessageBox.Show("please chose another date");
    }
}

public int TotalLines(string date)
{
    using (StreamReader r = new StreamReader(date))
    {
        int i = 0;
        while (r.ReadLine() != null) { i++; }
        return i;
    }     
}
于 2012-12-11T20:23:50.567 に答える
3

Kyle Uithoven のコードは正しいようです。TotalLinesメソッドを次のように置き換えることで、さらに単純化できます。

int counter = File.ReadLines(date).Count();
于 2012-12-11T20:26:22.640 に答える
0

C# If-else 構文:

if (condition)
{
    //Condition is true
    //Some commands
}
else
{
    //Condition is else
    //Some commands    
 }

button1_click 関数の外側で TotalLines 関数をより適切に定義します。

public int TotalLines(string date)
{
    using (StreamReader r = new StreamReader(date))
    {
        int i = 0;
        while (r.ReadLine() != null) { i++; }
        return i;
    }     
}
于 2012-12-11T20:27:23.867 に答える