C#第三章课后作业答案

  • 格式:pdf
  • 大小:97.66 KB
  • 文档页数:4

下载文档原格式

  / 4
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

while (true) { Console.Write("请输入一个长度大于3的字符串:"); string s1 = Console.ReadLine(); if (s1.Length == 0) continue; else if (s1.Length <= 3) { Console.WriteLine("输入有误,请从新输入!"); Console.WriteLine("\n"); continue; } else if (s1.Length > 3) { Console.WriteLine("(1)字符串{0}的长度为:{1}", s1, s1.Length); if (s1.Contains("a")) { int x = s1.IndexOf("a"); Console.WriteLine("(2)字符串{0}中含有字符a的位置为{1}", s1, x+1); } else Console.WriteLine("(2)字符串{0}中不含有字符a!", s1); string s2 = s1.Insert(3, "hellow"); Console.WriteLine("(3)在字符串{0}第3个字符后插入字符串【hellow】后得到的 字符串为:{1}", s1, s2); string s3 = s2.Replace("hellow", "me"); Console.WriteLine("(4)将字符串{0}中的字符串【hellow】替换为【me】后得到 的字符串为:{1}", s2, s3); int y = s3.IndexOf("m"); string s4 = s3.Substring(0, y); string s5 = s3.Substring(y); Console.WriteLine("(5)字符串{0}从m后分开得到两个字符串为:", s3); Console.WriteLine("字符串{0}的前一部分子字符串为:{1}", s3, s4); Console.WriteLine("字符串{0}的后一部分子字符串为:{1}", s3, s5); Console.ReadLine(); break; } else {
Console.WriteLine("输入有误,请从新输入!!!"); Console.WriteLine("\n"); break; }
} } } }
double x = Convert.ToInt32(Console.ReadLine()); if (n > 0) { 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); Console.ReadLine();break; } else { Console.WriteLine("输入有误!"); continue; } } } } }
要求精度为
using System;

using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { while (true) { Console.Write("请输入一个正整数n:"); double n = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入一个正整数x:");
第3章 常用数据类型的用法
1、C#语言中的数组类型有何特点? 【解答】 1)数组一般用于存储同一种类型的数据,包括 Object 类型。 2)数组是一种引用类型,而不是值类型。 3)C#中除了可以有一维数组、多维数组外,还有交错型数组。 2、泛型和非泛型的主要区别是什么?为什么说使用泛型比使用对应的非泛型效 率高? 【解答】泛型是数据类型的一种通用表示形式,它可以表示任何一种数据类型。 泛型(Generic)是具有占位符(类型参数)的类、结构、接口和方法,它与普 通类的区别是泛型多了一个或多个表示类型的占位符,这些占位符用尖括号括 起来。占位符表示某种类型,这种类型在创建泛型类的实例时才用实际类型来 替换。定义一个类或者方法时,可以用泛型占位符代表任何一种类型,而在引 用时再指定具体类型。当代码调用泛型类或方法时,C#编译器会自动将定义的 泛型转换为引用代码中指定的类型,从而大大简化了程序员编写代码的复杂度。 另外,由于占位符可以代表任何一种类型,因此在方法中只定义一次类型就能 实现所有类型的引用。 3、编写一个控制台应用程序,计算
Hale Waihona Puke Baidu
4、编写一个控制台应用程序,接收一个长度大于 3 的字符串,完成下列功能。 (1)输出字符串的长度。 (2)输出字符串中第一个出现字母 a 的位置。 (3)在字符串的第 3 个字符后面插入子串“hello”,输出新字符串。 (4)将字符串“hello”替换为“me”,输出新字符串。 (5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication3 { class Program { static void Main(string[] args) {