当前位置:编程学习 > C#/ASP.NET >>

在校大学生,求解一题

显示奇数【15分,编程题,建议项目Calculate】:使用一个定时器,按下“开始”按钮后开始输出从3开始的10个奇数,结果显示在多行文本框中。输出结果参照下图。 --------------------编程问答--------------------  int count = 0;
            int i = 3;
            while (count < 10)
            {
                if (i % 2 == 1)
                { 
                    this.listBox1.Items.Add(i); 
                    count++;
                }
                i++;
            } --------------------编程问答-------------------- 最前面加这一句: this.listBox1.Items.Clear(); --------------------编程问答--------------------

引用 1 楼 likedefly 的回复:
int count = 0;
            int i = 3;
            while (count < 10)
            {
                if (i % 2 == 1)
                { 
                    this.listBox1.Items.Add(i)……
--------------------编程问答--------------------
int s = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {
            listBox1.Items.Add(s);
            s += 2;
        }
--------------------编程问答-------------------- int res=1;
private void btnStart_Click(object sender, EventArgs e)     
{    
    System.Timers.Timer timer = new System.Timers.Timer();//实例化Timer类                                             
    timer.Interval = 1000;//设置间隔时间            
     timer.Elapsed += new System.Timers.ElapsedEventHandler(domywork);//到达时间的时候执行事件
     timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);        
     timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;       
 } 

public void domywork(object source, System.Timers.ElapsedEventArgs e)        {         
   listBox1.Items.Add(s);             
   res += 2; 
} --------------------编程问答-------------------- 计算器的作用呢? --------------------编程问答-------------------- 刚没看清题目,不好意思。
private void Form2_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            Thread th = new Thread(new ThreadStart(addNum));
            th.Start();
        }

        private void addNum()
        {
            int count = 0;
            int i = 3;
            while (count < 10)
            {
                if (i % 2 == 1)
                {
                    Thread.Sleep(1000);
                    this.listBox1.Items.Add(i);
                    count++;
                }
                i++;
            }
        }

--------------------编程问答--------------------

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

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

        int i = 0;
        int num = 2;
        private void button1_Click(object sender, EventArgs e)
        {
            //初始化
              i = 0;
            num = 2;
            richTextBox1.Text = "";

            //timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if ((++num) % 2 == 1)
            {
                if (i < 10 )
                {
                    richTextBox1.Text += num + "\n";
                    i++;
                }
                else
                {
                    timer1.Stop();
                }
            }
        }
    }
}

--------------------编程问答-------------------- int s = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {
            s += 2;
            listBox1.Items.Add(s);
            if (s > 21) timer1.Enabled = false;
        } --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace CsdnApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int count = 0;   //奇数个数
        private int number = 3;  //开始奇数 &&timer1_Tick判断开始的奇数

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
            //定义定时器
            this.timer1.Tick += new EventHandler(timer1_Tick);
            this.timer1.Interval = 1000; //定义时间间隔
            this.timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = number; count < 10; i++)
            {
                if (i % 2 != 0)
                {
                    textBox1.Text += i.ToString() + "\r\n";
                    count++;
                    number = i+1;
                    break;
                }
            }
            if (count>=10)
            {
                this.timer1.Stop();
            }
        }
    }
}
--------------------编程问答-------------------- 看到楼主的结贴记录,我无语了 --------------------编程问答-------------------- 问题表达得不是很清楚吧?? --------------------编程问答-------------------- 退学算了。。 --------------------编程问答-------------------- 代码简洁,效率搞,才是值得学习。模仿版主吧。 --------------------编程问答-------------------- int[] a ={3,5,7,9,11,......}

--------------------编程问答-------------------- --------------------编程问答--------------------
引用 13 楼 conmajia 的回复:
退学算了。。


在校学生本来就不能当作正常的开发人员。csdn没有区分学生跟开发人员两类,我们还是容忍学生算了。 --------------------编程问答-------------------- sp1234说的对,想当初谁不是从学生走过来的,CSDN是技术交流区,所有爱好编程的人都可以进行交流,大家共同探讨问题,共同进步! --------------------编程问答-------------------- 呵呵,是呀,这道题不是很难呀。比打印不规则图形要简单,比递归要简单,比打印杨辉三角要简单吧。呵呵。我也是从上学过来的。 --------------------编程问答-------------------- --------------------编程问答-------------------- 野比也是从菜鸟过来的。 --------------------编程问答-------------------- 目测LZ是不会用定时器 --------------------编程问答-------------------- 本人初学,没基础,还望前辈多多指教! --------------------编程问答-------------------- 目测各位的程序好像定时器只会动一次的吧。
每次时间到应该再启动定时器,才能开始下一次定时的 --------------------编程问答--------------------
引用 24 楼 a3212b12 的回复:
目测各位的程序好像定时器只会动一次的吧。
每次时间到应该再启动定时器,才能开始下一次定时的


刚才试验了下,我的说法错了,定时器只要一次启动不停止的话可以连续定时的。这样的话用定时器可能有风险,就是时间到后的处理程序的时间如果过长,当下次到时的时候上次的事务还没有结束的话,哈哈--那会出现什么后果,我还没有试验过。

下面是本人关于1楼的vb代码:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.RichTextBox1.Text = ""
        Me.Timer1.Interval = 1000
        Me.Timer1.Start()

    End Sub

    Private v_num As Int16 = 3
    Private v_ii As Int16 = 1
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.RichTextBox1.Text = Me.RichTextBox1.Text & v_num & vbNewLine
        v_num = v_num + 2
        v_ii = v_ii + 1
        If v_ii >= 11 Then
            Me.Timer1.Stop()
        Else
            'Me.Timer1.Start()
        End If


    End Sub
End Class
'Me.Timer1.Start() 一句注销和不注销结果一样。


--------------------编程问答-------------------- 效果
--------------------编程问答-------------------- --------------------编程问答-------------------- 别问代码问思路吧以后 --------------------编程问答--------------------
引用 25 楼 a3212b12 的回复:
引用 24 楼 a3212b12 的回复:目测各位的程序好像定时器只会动一次的吧。
每次时间到应该再启动定时器,才能开始下一次定时的

刚才试验了下,我的说法错了,定时器只要一次启动不停止的话可以连续定时的。这样的话用定时器可能有风险,就是时间到后的处理程序的时间如果过长,当下次到时的时候上次的事务还没有结束的话,哈哈--那会出现什么后果,我还没有试验过。

下……


次代码是有个BUG的,留给楼主去排查了 --------------------编程问答--------------------
引用 1 楼 likedefly 的回复:
int count = 0;
            int i = 3;
            while (count < 10)
            {
                if (i % 2 == 1)
                { 
                    this.listBox1.Items.Add(i); 
         ……



 int count = 0;
            int i = 3;
            while (count < 10)
            {

                    this.listBox1.Items.Add(i); 
                    i+=2;
count ++;
            } 
你写的那个明显不对啊 --------------------编程问答--------------------
引用 30 楼 wjfwd2010 的回复:
引用 1 楼 likedefly 的回复:
int count = 0;
            int i = 3;
            while (count < 10)
            {
                if (i % 2 == 1)
                { 
                    this.listBox1.Ite……

性能太差还走了弯路。。。 --------------------编程问答-------------------- 搞那么复杂你们, 直接告诉他一些, 核心代码就完事了 ,至于 Timer 的用法,叫他自己研究吧。要不你们会害了他的。


if(i%2 ==0)
{

}
else
{

}

自己做就可以了啊, 

System.Timers.Timer
System.Windows.Forms.Timer
System.Thread.Timer

3个Timer 你自己选择,用法大有不同啊。 --------------------编程问答-------------------- 既然是学生,就好好学习,又不是很难的东西。 --------------------编程问答-------------------- 也就是一个简单的小程序,也可以理解哦 --------------------编程问答--------------------
引用 9 楼 caozhy 的回复:
int s = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {
            s += 2;
            listBox1.Items.Add(s);
            if (s > 21) timer1.Enabled = fals……

膜拜楼主ing= =确实非常简洁明了的说~~~ --------------------编程问答-------------------- 回帖,拿分,闪人! --------------------编程问答-------------------- 浪费青春。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,