




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
4、分别写出下列语句执行的结果。1) Console.WriteLine(0-0:pgood,12.34F);2) Console.WriteLine(0-0:#good,0);3) Console.WriteLine(0-0:00000good,456);【解答】12.34-1,234.00%good0-good456-00456good5、编写一个控制台应用程序,输出1到5的平方值,要求: 1) 用for语句实现。 2) 用while语句实现。 3) 用do-while语句实现。【解答】using System;using System.Collections.Generic;using System.Text;namespace outputSquareValue class Program static void Main() /用for语句实现 for (int i = 1; i = 5; i+) Console.WriteLine(0的平方值为1, i, i * i); /用while语句实现 int j = 0; while (j+ 5) Console.WriteLine(0的平方值为1, j, j * j); /用do-while语句实现 int k = 1; do Console.WriteLine(0的平方值为1, k, k * k); while (k+ 5); Console.ReadLine(); 6、编写一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。【解答】using System;using System.Collections.Generic;using System.Text;namespace inputCapitalLetter class Program static void Main() bool ok = false; while (ok = false) Console.Write(请输入5个大写字母:); string str = Console.ReadLine(); if (str.Length != 5) Console.WriteLine(你输入的字符个数不是5个,请重新输入。); else ok = true; for (int i = 0; i 5; i+) char c = stri; if (c Z) Console.WriteLine(第0个字符“1”不是大写字母,请重新输入。, i + 1, c); ok = false; break; 7、编写一个控制台应用程序,要求完成下列功能。 1) 接收一个整数n。 2) 如果接收的值n为正数,输出1到n间的全部整数。 3) 如果接收的值为负值,用break或者return退出程序。 4) 转到(1)继续接收下一个整数。【解答】using System;using System.Collections.Generic;using System.Text;namespace testOutput class Program static void Main() while (true) Console.Write(请输入一个整数(负值结束):); string str = Console.ReadLine(); try int i = Int32.Parse(str); if (i 0) break; for (int j = 1; j = i; j+) Console.WriteLine(j); catch Console.WriteLine(你输入的不是数字或超出整数的表示范围,请重新输入); 8、编写一个控制台应用程序,求1000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如,6是完数,因为6=1+2+3。【解答】using System;using System.Collections.Generic;using System.Text;namespace completeNumber class Program static void Main(string args) for (int i = 2; i = 1000; i+) int s = 1; string str = 1; for (int j = 2; j = (int)Math.Sqrt(i); j+) if (j * (i / j) = i) if (j != i / j) s += j + i / j; str += string.Format(+0+1, j, i / j); else s += j; str += string.Format(+0, j); if (s = i) Console.WriteLine(0=1, i, str); Console.ReadLine(); 3、编写一个控制台应用程序,计算要求精度为108。【解答】using System;class Test3 public static void Main() int n = 50; double x = 3; double s = 0; double a = 1; for (int i = 1; i = n; i+) a *= i; s += Math.Pow(-1, i + 1) * Math.Pow(x, i) / a; Console.WriteLine(n=0,s=1:0.00000000, n, s); 4、编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能。(1)输出字符串的长度。(2)输出字符串中第一个出现字母a的位置。(3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。(4)将字符串“hello”替换为“me”,输出新字符串。(5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。【解答】【解答】using System;class Test4 public static void Main() string str = ; while (str.Length -1) Console.WriteLine(第一个出现字母a的位置是:0, i); else Console.WriteLine(字符串中不包含字母a。); /(3) string str1 = str.Insert(3, hello); /在第3个(初始序号为)字符前插入hello Console.WriteLine(插入hello后的结果为:0, str1); /(4) string str2 = str1.Replace(hello, me); Console.WriteLine(将hello替换为me后的结果为:0, str2); /(5) string arr = str2.Split(m); Console.WriteLine(以m为分隔符分离后的字符串有:); for (int j = 0; j arr.Length; j+) Console.WriteLine(arrj); 1、编写一个控制台应用程序,完成下列功能。(1)创建一个类,用无参数的构造函数输出该类的类名。(2)增加一个重载的构造函数,带有一个string类型的参数,在此构造函数中将传递的字符串打印出来。(3)在Main方法中创建属于这个类的一个对象,不传递参数。(4)在Main方法中创建属于这个类的另一个对象,传递一个字符串“This is a string.”。(5)在Main方法中声明类型为这个类的一个具有5个对象的数组,但不要实际创建分配到数组里的对象。(6)写出运行程序应该输出的结果。【解答】using System;class Test1 public Test1() Console.WriteLine(this); public Test1(string str) Console.WriteLine(str); public static void Main() Test1 t1 = new Test1(); Test1 t2 = new Test1(This is a string.); Test1 t3 = new Test15; 输出结果:Test1This is a string.2、编写一个控制台应用程序,定义一个类MyClass,类中包含有public、private以及protected数据成员及方法。然后定义一个从MyClass类继承的类MyMain,将Main方法放在MyMain中,在Main方法中创建MyClass类的一个对象,并分别访问类中的数据成员及方法。要求注明在试图访问所有类成员时哪些语句会产生编译错误。【解答】using System;class MyClass public int i; private int j; protected int k; public void method1() Console.WriteLine(public method.); private void method2() Console.WriteLine(private method.); protected void method3() Console.WriteLine(protected method.); class MyMain : MyClass public static void Main() MyClass t = new MyClass(); Console.WriteLine(i=0, t.i); Console.WriteLine(j=0, t.j);/会出现编译错误,私有成员不允许在其它类中访问 Console.WriteLine(k=0, t.k);/会出现编译错误,应该创建MyMain的对象,然 /后通过MyMain的对象访问 t.method1(); t.method2(); /会出现编译错误,私有的方法不允许在其它类中调用 t.method3(); /会出现编译错误,应该创建MyMain的对象,然后通过MyMain的 /对象调用该方法 3、创建一个类包含有protected数据。在相同的文件里创建第二个类,用一个方法操纵第一个类里的protected数据。【解答】using System;class Class1 protected int i = 5; protected void MyMethod() Console.WriteLine(protected method.); class Class2 : Class1 private void NewMethod() Console.WriteLine(this.i); this.i += 10; Console.WriteLine(this.i); public static void Main() Class2 t = new Class2(); t.NewMethod(); 3、编写一个控制台应用程序,完成下列功能,并回答提出的问题。(1)创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。(2)从A继承一个名为C的新类,并在C内创建一个成员B。不要为C创建构造函数。(3)在Main方法中创建类C的一个对象,写出运行程序后输出的结果。(4)如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?【解答】using System;public class A public A() Console.WriteLine(A); public class B public B() Console.WriteLine(B); public class C : A B newb = new B();class MainClass public static void Main() C newc = new C(); Console.ReadLine(); 输出结果:BA 如果在C中也创建一个构造函数输出“C”,即添加:public C()Console.WriteLine(C); 则整个程序运行的结果为:BAC4、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。(1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值加10后的结果。(2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。(3)在Main方法中分别创建类A和类B的对象,并分别调用MyMethod方法。【解答】using System;public class A public virtual void MyMethod(int num) num += 10; Console.WriteLine(num); public class B : A public override void MyMethod(int num) num += 50; Console.WriteLine(num); class MainClass public static void Main() A newa = new A(); newa.MyMethod(2); B newb = new B(); newb.MyMethod(2); Console.ReadLine(); 输出结果:12525、假设Node类的每一个节点包括有两个字段:m_data(引用节点的数据)和m_next(引用链接列表中的下一项)。这两个字段都是由构造函数方法设置的。该类有两个功能,第一个功能是通过名为Data和Next的只读属性访问m_data和m_next字段。第二个功能是对System.Object的ToString虚拟方法进行重写。试分别用类和泛型两种方法编写程序实现上述功能。【解答】using System;class Node Object m_data; Node m_next; public Node(Object data, Node next) m_data = data; m_next = next; / 访问结点数据 public Object Data get return m_data; / 访问下一个结点 public Node Next get return m_next; / 获取结点数据描述 public override String ToString() return m_data.ToString(); / 链表结点类的泛型定义class Node T m_data; Node m_next; public Node(T data, Node next) m_data = data; m_next = next; / 访问结点数据 public T Data get return m_data; set m_data = value; / 访问下一个结点 public Node Next get return m_next; set m_next = value; / 获取结点数据描述 public override String ToString() return m_data.ToString(); / 使用结点类型或泛型结点类型class LinkedList static void Main(string args) / 创建整数链表 /Node head = new Node(5, null); /head = new Node(10, head); /head = new Node(15, head); /遍历链表求整数和 /Int32 sum = 0; /for (Node current = head; current != null; / current = current.Next) / / sum += (Int32)current.Data; / / 输出结果 /Console.WriteLine(Sum of nodes = 0, sum); / 用泛型创建整数链表 Node head = new Node(5, null); head = new Node(10, head); head = new Node(15, head); / 遍历求和 Int32 sum = 0; for (Node current = head; current != null; current = current.Next) sum += current.Data; / 输出 Console.WriteLine(Sum of nodes = 0, sum.ToString(); 4、设计一个Windows应用程序,窗体上有一个TextBox控件、一个Button控件。要求:每当用户单击按钮时,文本框都会增加一行文字来反映单击的次数,例如,“第3次单击按钮”。【解答】1) 窗体界面如图6-1所示;2) 窗体中主要控件属性设置如表6-1;表6-1 窗体中的主要控件属性控件Name属性功能其它属性TextBox控件textBox1显示信息ScrollBars=Vertical; Multiline=TrueButton控件Button1触发添加信息事件Button2触发结束添加事件图6-1 窗体界面 3) 主要事件代码。int i = 1;bool Add = true;private void button1_Click(object sender, EventArgs e) if(Add) textBox1.Text += 第 + i + 次单击按钮rn; i+;private void button2_Click(object sender, EventArgs e) Add = false;5、编写一段程序,向名为listBox1的ListBox控件中,自动添加10个随机数,每个数占一项。【解答】 主要代码如下。public partial class Form1 : Form int m = 1; private void button1_Click(object sender, EventArgs e) for (int i = m ; i m+10; i+) listBox1.Items.Add(i); m = m + 10; 2、编写程序用Directory类提供的方法确定指定的目录是否存在,如果不存在,则创建该目录。然后在其中创建一个文件,并将一个字符串写到文件中。【解答】程序清单如下:using System;using System.IO;class Test public static void Main() string path = c:MyDir; try if (!Directory.Exists(path) Directory.CreateDirectory(path); StreamWriter sw=File.CreateText(path+myfile.txt); sw.WriteLine(This is a String!); sw.close(); catch (Exception e) Console.WriteLine(操作失败: 0, e.ToString(); finally 3、编写程序,使用File类实现删除指定目录下的指定文件。【解答】程序清单如下:using System;using System.IO;class FileTestpublic static void Main()Console.WriteLine(确认删除当前目录下的所有文件?);Console.WriteLine(点击Y键继续,其它键取消操作);int a = Console.Read();if(a = Y | a = y)Console.WriteLine(正在删除文件.);elseConsole.WriteLine(用户取消操作);return;DirectoryInfo dir = new DirectoryInfo (.);foreach (FileInfo f in dir.GetFiles()f.Delete();2、使用Panel控件分别以矩形、椭圆和圆形的方式动态显示图片,图片的大小由Panel控件的大小决定。【解答】(1)新建一个Windows应用程序,命名为“ShowImageExe”,调整窗体到适当大小。更改“Form1.cs”为“FromShowImageExe.cs”。(2)切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;(3)添加四个Button控件分别命名为“buttonOpenFile”、“buttonRectangle”、“buttonEllipse”、“buttonRound”,以及一个openFileDiolog和Panel控件。(4)在Form类下声明两个私有变量filename和flag,分别用来记录打开的文件名和判断哪个按钮的click时间被触发。private string filename = ;private int flag = 0;(5) 添加【打开文件】按钮的click事件private void buttonOpenFile_Click(object sender, EventArgs e) openFileDialog1.ShowDialog(); filename = openFileDialog1.FileName; panel1.Refresh();(6) 在panel1控件的paint事件下添加如下代码:private void panel1_Paint(object sender, PaintEventArgs e) if (filename.Trim() = )return;Bitmap mybitmap = new Bitmap(filename); Graphics g = e.Graphics; TextureBrush mybrush = new TextureBrush(mybitmap,WrapMode.Clamp);/保证图片完全由绘制对象的边框决定 switch (flag) case 1: g.FillRectangle(mybrush, panel1.ClientRectangle); break; case 2: g.FillEllipse(mybrush, panel1.ClientRectangle); break; case 3: g.FillEllipse(mybrush,(panel1.Width-panel1.Height)/2,0, panel1.Height,panel1.Height); break; (7) 在其他几个按钮的click事件中分别添加如下代码:private void buttonRectangle_Click(object sender, EventArgs e) flag = 1; panel1.Refresh();private void buttonEllipse_Click(object sender, EventArgs e) flag = 2; panel1.Refresh();private void buttonRound_Click(object sender, EventArgs e) flag = 3; panel1.Refresh(); (8) 结果如图所示。第2题 以矩形、椭圆和圆形的方式显示图片运行图3、编写一个Windows窗体应用程序,利用PictureBox控件和Panel控件实现滚动浏览大图片的功能。【解答】由于Picturebox控件在显示图片时不能直接使用滚动条,所以必须借助Panel控件实现以滚动条的方式浏览大图片。具体操作步骤如下:(1)新建一个Windows应用程序,命名为“scrollBar”,调整窗体到适当大小。更改“Form1.cs”为“FormScrollBar.cs”。(2)切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;(3)在窗体上分别添加一个button控件命名为“buttonOpenFile”,一个openFileDiolog控件,Picturebox和Panel控件各一个,将Panel控件的AutoScroll属性设为true。(4) 在“buttonOpenFile”控件的click事件中添加如下代码:private void buttonOpenFile_Click(object sender, EventArgs e)openFileDialog1.ShowDialog();if (openFileDialog1.FileName.Trim() = ) return;try Bitmap mybitmap = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = mybitmap; catch (Exception Err) MessageBox.Show(打开文件错误!, 信息提示, MessageBoxButtons.OK, MessageBoxIcon.Information); (5)结果如图所示。第3题 滚动条浏览大图片4、编写一个Windows窗体应用程序,实现对图片进行旋转、缩放和平移的功能。【解答】(1)在窗体上添加六个label控件(其中label1用来显示图片)、一个button控件(用于打开图片文件)和五个numericUpDown控件(分别用来选择图片缩放的比例、图片旋转的角度、图片位移的大小)。(2)在构造函数上方添加代码:private string strfilename=;(3)在button控件的click事件里添加如下代码:private void button1_Click(object sender, EventArgs e)openFileDialog1.ShowDialog();strfilename=openFileDialog1.FileName;label1.Refresh();(4)在每一个numericUpDown控件的ValueChanged事件中添加如下代码:label1.Refresh();(5)在label1控件的paint事件中添加如下代码:private void label1_Paint(object sender, PaintEventArgs e) if (this.strfilename.Trim()=) return ; try Bitmap mybitmap = new Bitmap(strfilename); Graphics g = e.Graphics; TextureBrush mybrush = new TextureBrush(mybitmap); float x = (float)(numericUpDownS1.Value / 100); float y = (float)(numericUpDownS2.Value / 100); mybrush.ScaleTransform(x, y); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); float r = (float)(numericUpDownR1.Value); mybrush.RotateTransform(r); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); float tx = (float)(numericUpDownT1.Value); float ty = (float)(numericUpDownT2.Value); mybrush.TranslateTransform(tx, ty); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); catch (Exception Err) MessageBox.Show(打开文件错误!, 信息提示, MessageBoxButtons.OK, MessageBoxIcon.Information); 1、使用保持连接方式编写程序,计算各年级平均成绩,并显示结果。【解答】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 习题9_1 public partial class Form1 : Form public Form1() InitializeComponent(); /添加Button按钮在ListBox中显示结果 private void button1_Click(object sender
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全、文明施工方案
- 河南省漯河市郾城区2022-2023学年九年级上学期期中化学试题(含答案)
- 高电压试验基础知识培训课件
- 9Z-11E-Octadecadienoyl-CoA-9Z-11E-Octadecadienoyl-coenzyme-A-生命科学试剂-MCE
- 保险金融资格考试科目及答案
- 保险代理人分级考试题及答案
- 高桥村消防知识培训课件
- 高校无人机培训课件
- 高志谦课件教学课件
- 高尔夫球基础知识培训课件
- 电工电子基础知识培训课件
- 骨髓炎诊断与治疗
- “满鲜一体化”视域下“满鲜”商业会议所联合会研究(1918-1929)
- 小学生AI科普课件
- 2025新食品安全法及修订解读企业应对新规培训课件
- DGJ08-70-2021 建筑物、构筑物拆除技术标准
- 新时代基础教育改革政策解读
- 中国足协D级教练员理论培训大纲
- 2025年4月自考00611日语句法篇章法试题
- 部编五年级上册语文教案全册表格版
- 儿科绪论考试题及答案
评论
0/150
提交评论