| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1789 人关注过本帖
标题:[求助]如何用C#写出在控制台上输出1到100的奇数。
只看楼主 加入收藏
C中毒-唐朝
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-4-9
收藏
 问题点数:0 回复次数:10 
[求助]如何用C#写出在控制台上输出1到100的奇数。
如何用C#写出在控制台上输出1到100的奇数。
请各位高手帮帮忙啊!!!!!!在此谢了!
搜索更多相关主题的帖子: 奇数 控制台 输出 
2005-06-09 15:10
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
0%2=?
1%2=?

2005-06-10 10:31
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
二楼的第二行写错了,应该是这样:   for(int i = 1;i<=100;i++)
0%2=0
1%2=1
2%2=0
3%2=1
求余

寻求挑战,追求完美 Oh,my god!
2005-06-14 06:43
生命如此多情
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-6-22
收藏
得分:0 
都这么厉害啊!

生命可以无所谓而来,却不可以无所谓而去。
2005-06-22 16:09
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 
这都是些不能不掌握的基本知识,不会的朋友快加油啊!

寻求挑战,追求完美 Oh,my god!
2005-07-01 23:45
♂星里有树♂
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-7-7
收藏
得分:0 
这段是输出奇数。。。
i%2==0

CONTINIU
2005-07-07 14:51
tuablove
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2005-7-11
收藏
得分:0 
Console.WriteLine("1至100的奇数为:"); for(int i = 0;i<100;i++) { if(i%2 == 0) { continue; } else { Console.WriteLine("{0}",i); } } Console.WriteLine(" 质数=? "); for(int i=0;i<=100;i++) { if(i%2==0) continue else Console.WriteLine("{0}",i) } 这样对么?

〖石器时代〗来到现代,不好混啊.... @==)(=======> 为生存而拼搏
2005-07-11 02:37
houyunqing
Rank: 1
等 级:新手上路
帖 子:476
专家分:0
注 册:2005-4-1
收藏
得分:0 

你是说质数?那上面的你肯定错了啦~我做过一个质合计算器,方法很蠢,但还可以用... 有兴趣的话你可以读一下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace 质合计算器 { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.NumericUpDown Numbox; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox text; private System.Windows.Forms.ContextMenu contextMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.Panel panel1; private System.ComponentModel.IContainer components;

public Form1() { // // Required for Windows Form Designer support // InitializeComponent();

// // TOD Add any constructor code after InitializeComponent call // }

/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }

#region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.label1 = new System.Windows.Forms.Label(); this.Numbox = new System.Windows.Forms.NumericUpDown(); this.text = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.panel1 = new System.Windows.Forms.Panel(); ((System.ComponentModel.ISupportInitialize)(this.Numbox)).BeginInit(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(16, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(112, 16); this.label1.TabIndex = 0; this.label1.Text = "要进行判断的数字:"; this.toolTip1.SetToolTip(this.label1, "注意:计算器的最大值为2147483647"); // // Numbox // this.Numbox.ContextMenu = this.contextMenu1; this.Numbox.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.Numbox.ForeColor = System.Drawing.Color.Orange; this.Numbox.Location = new System.Drawing.Point(128, 16); this.Numbox.Maximum = new System.Decimal(new int[] { 2147483647, 0, 0, 0}); this.Numbox.Name = "Numbox"; this.Numbox.Size = new System.Drawing.Size(160, 23); this.Numbox.TabIndex = 1; this.Numbox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.toolTip1.SetToolTip(this.Numbox, "注意:计算器的最大值为2147483647"); this.Numbox.Value = new System.Decimal(new int[] { 2, 0, 0, 0}); // // text // this.text.BackColor = System.Drawing.SystemColors.ControlLightLight; this.text.ContextMenu = this.contextMenu1; this.text.ForeColor = System.Drawing.Color.MediumSpringGreen; this.text.Location = new System.Drawing.Point(64, 48); this.text.Name = "text"; this.text.ReadOnly = true; this.text.Size = new System.Drawing.Size(224, 21); this.text.TabIndex = 2; this.text.Text = ""; this.toolTip1.SetToolTip(this.text, "注意:计算器的最大值为2147483647"); // // label2 // this.label2.Location = new System.Drawing.Point(24, 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(40, 24); this.label2.TabIndex = 3; this.label2.Text = "结果:"; this.toolTip1.SetToolTip(this.label2, "注意:计算器的最大值为2147483647"); // // button1 // this.button1.Location = new System.Drawing.Point(96, 80); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 24); this.button1.TabIndex = 4; this.button1.Text = "判断"; this.toolTip1.SetToolTip(this.button1, "注意:计算器的最大值为2147483647"); this.button1.Click += new System.EventHandler(this.button1_Click); // // contextMenu1 // this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "关于"; this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // // panel1 // this.panel1.Controls.Add(this.button1); this.panel1.Controls.Add(this.label2); this.panel1.Controls.Add(this.text); this.panel1.Controls.Add(this.Numbox); this.panel1.Controls.Add(this.label1); this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(304, 136); this.panel1.TabIndex = 5; this.toolTip1.SetToolTip(this.panel1, "注意:计算器的最大值为2147483647"); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(306, 112); this.ContextMenu = this.contextMenu1; this.Controls.Add(this.panel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MaximumSize = new System.Drawing.Size(312, 144); this.MinimumSize = new System.Drawing.Size(312, 144); this.Name = "Form1"; this.Text = "质合计算器"; ((System.ComponentModel.ISupportInitialize)(this.Numbox)).EndInit(); this.panel1.ResumeLayout(false); this.ResumeLayout(false);

} #endregion

/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }

private void button1_Click(object sender, System.EventArgs e) { int num = (int)Numbox.Value; int yuannum = num; if(num<=1) { MessageBox.Show("小于或者等于一的整数都不是质数或者合数","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } if(num==2) { text.Text="2是一个质数"; return; } //对特殊例子的处理完成! if(num>999999999) { DialogResult ok =MessageBox.Show("如果"+num.ToString()+"是一个质数,计算器可能会要花相当长的时间进行计算,确认要继续吗?","是否继续",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); if(ok!=DialogResult.OK) return; } //接下来进行真正的算法! ArrayList numlst = new ArrayList(); bool doesit = false; for(int x=2;x<num/2+1;x++) { if(num==x) break; if(num%x==0) { numlst.Add(x); num=num/x; doesit = true; if(x==4) { MessageBox.Show("num:"+num.ToString(),"4"); } x=1; } } if(doesit==false) { text.Text=yuannum.ToString()+"是一个质数"; } else { string log = null; for(int x=0;x<numlst.Count;x++) { log+=((int)numlst[x]).ToString()+"*"; } text.Text=yuannum.ToString()+"是一个合数,"+yuannum.ToString()+"="+log+num.ToString(); } }

private void menuItem1_Click(object sender, System.EventArgs e) { MessageBox.Show(" 作者:Geoge小猴儿 \n QQ:362938909 \n E-mail:houyunqing@yeah.net \n 闲着没事写了这个东东,它有时候还真有点用哦!! \n 注意:计算器可以输入的最大值为"+int.MaxValue.ToString(),"关于"); } } }


寻求挑战,追求完美 Oh,my god!
2005-07-13 17:49
Benjamin
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2005-7-11
收藏
得分:0 
哇,不错嘛!

关键还是算法

小弟是新手,请各位哥哥姐姐帮忙! 吾将不胜感激……
2005-07-15 13:18
C中毒-唐朝
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-4-9
收藏
得分:0 
佩服!佩服!
谢谢各位的帮忙!!!!!

2005-08-06 17:25
快速回复:[求助]如何用C#写出在控制台上输出1到100的奇数。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025245 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved