当前位置:首页 > C#第3版第2章习题解答
Ch2 基本数据类型与流程控制语句 习题解答
1. 简要回答值类型和引用类型有何不同? 【解答】
值类型和引用类型的区别在于,值类型的变量直接存放实际的数据,而引用类型的变量存放的则是数据的地址,即对象的引用。当把一个值变量赋给另一个值变量时,会在堆栈(stack)中保存两个完全相同的值;而把一个引用变量赋给另一个引用变量,则会在堆栈中保存对同一个堆(heap)位置的两个引用。进行数据操作时,对于值类型,由于每个变量都有自己的值,因此对一个变量的操作不会影响到其他变量;对于引用类型的变量,对一个变量的数据进行操作就是对这个变量在堆中的数据进行操作,如果两个引用类型的变量引用同一个对象,实际含义就是它们在堆栈中保存的堆的地址相同,因此对一个变量的操作就会影响到引用同一个对象的另一个变量。
2. C#语言中不同整型之间进行转换的原则是什么? 【解答】
在整型之间进行转换时,小范围类型可以隐式转换为大范围类型,但大范围类型转换为小范围类型时需要使用显式转换。
3、编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能。 (1)输出字符串的长度。
(2)输出字符串中第一个出现字母a的位置。
(3)字符串序号从零开始编号,在字符串的第3个字符的前面插入子串“hello”,输出新字符串。
(4)将字符串“hello”替换为“me”,输出新字符串。
(5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 【解答】
using System; class T3 {
public static void Main() {
string str = \
while (str.Length <= 3) {
Console.Write(\请输入一个长度大于3的字符串:\ str = Console.ReadLine(); }
//(1)
Console.WriteLine(\字符串的长度为:{0}\ //(2)
int i = str.IndexOf('a'); if (i > -1) {
Console.WriteLine(\第一个出现字母a的位置是:{0}\
2
第2章 基本数据类型与流程控制语句 } else {
Console.WriteLine(\字符串中不包含字母a。\ }
//(3)
string str1 = str.Insert(3, \ //在第3个(初始序号为)字符前插入hello Console.WriteLine(\插入hello后的结果为:{0}\ //(4)
string str2 = str1.Replace(\
Console.WriteLine(\将hello替换为me后的结果为:{0}\ //(5)
string[] arr = str2.Split('m');
Console.WriteLine(\以m为分隔符分离后的字符串有:\ for (int j = 0; j < arr.Length; j++) {
Console.WriteLine(arr[j]); }
Console.ReadLine(); } }
4.编写一个控制台应用程序,输出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}\ }
//用while语句实现 int j = 0;
while (j++ < 5) {
Console.WriteLine(\{0}的平方值为{1}\ }
//用do-while语句实现 int k = 1; do {
3
第2章 基本数据类型与流程控制语句 Console.WriteLine(\{0}的平方值为{1}\ } while (k++ < 5); Console.ReadLine(); } } }
5.编写一个控制台应用程序,要求用户输入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 = str[i]; if (c < 'A' || c > 'Z') {
Console.WriteLine(\第{0}个字符“{1}”不是大写字母,请重新输入。\
ok = false; break; } } } } } } }
6.编写一个控制台应用程序,要求完成下列功能。 (1)接收一个整数n。
(2)如果接收的值n为正数,输出1~n的全部整数。
(3)如果接收的值为负值,用break或者return退出程序。
4
第2章 基本数据类型与流程控制语句 (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(\你输入的不是数字或超出整数的表示范围,请重新输入\
} } } } }
7.编写一个控制台应用程序,求1 000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如,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 = \
for (int j = 2; j <= (int)Math.Sqrt(i); j++) {
if (j * (i / j) == i)
5
共分享92篇相关文档