C#第一次实验报告

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

1.Write an application that includes the logic from Exercise 1 (TEXT

BOOK “Beginning Microsoft Visual C# 2008”, Page 67), obtains two numbers from the user, and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers.

Key code:

static void Main(string[] args)

{

int n, m;

Console.WriteLine("Enter two integer:");

n = Convert.ToInt32(Console.ReadLine());

m = Convert.ToInt32(Console.ReadLine());

while (n < 10 || m < 10)

{

if (n < 10)

Console.WriteLine("n = {0} greater than 10.", n);

if (m < 10)

Console.WriteLine("m = {0} greater than 10.", m);

Console.WriteLine("Enter two integer:");

n = Convert.ToInt32(Console.ReadLine());

m = Convert.ToInt32(Console.ReadLine());

}

Console.WriteLine("n and m are not greater than 10.\nBye!");

Console.ReadKey();

}

Output:

2.Write an application that display the multiplication table (九九乘法

表).

Keycode:

static void Main(string[] args)

{

for (int i = 1; i <= 9; i++)

{

for (int j = 1; j <= i; j++)

{

Console.Write("{0} * {1} = {2} ",j, i, i * j); }

Console.WriteLine();

}

Console.WriteLine();

Console.ReadKey();

}

Output:

3.Write a console application that accepts a string from the user and

outputs a string with the characters in reverse order.

Keycode:

static void Main(string[] args)

{

string str;

str = Console.ReadLine();

int len = str.Length;

for (int i = len - 1; i >= 0; i--)

{

Console.Write("{0}", str[i]);

}

Console.WriteLine();

Console.ReadKey();

}

Output:

4.Write a console application that accepts a string and replaces all

occurrences of the string no with yes

Keycode:

static void Main(string[] args)

{

string str = Console.ReadLine();

str = str.Replace("no", "yes");

Console.WriteLine(str);

Console.ReadKey();

}

Output:

5.Write a console application that places double quotes around each

word in a string.

Keycode:

static void Main(string[] args)

{

string str = Console.ReadLine();

str = str.Replace(" ", "\" \"");

string s = "\"" + str + "\"";

Console.WriteLine(s);

Console.ReadKey();

}

Output:

相关文档
最新文档