0

我有一个想要的项目

  • 返回当前计数的方法。
  • 将计数设置为零的构造函数。

我有前几个,但需要帮助返回计数为 0,然后是构造函数。我需要通过添加一个计数器类来做到这一点,但我对添加它的方式感到困惑。有人可以帮我吗?

using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms;

namespace Project10TC 
{
    public partial class Form1 : Form    
    {
        int zero = 0;
        int i = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private EventHandler myCounter;

        // end of Form class

        private class myCounter()
        {
             myCounter = new myCounter( );
        }

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

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = (++i).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = (--i).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = (zero).ToString();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
4

1 回答 1

2
public class Counter
{
    public int Value { get; private set; }

    public void Increment() 
    {
       Value = Value + 1;
    }

    public void Decrement() 
    {
      if (Value > 0) Value = Value - 1;
    }

    public Counter()
    {
       Value = 0;
    }
 }
于 2012-12-04T04:12:32.520 回答