零基础学会C#二——枚举&浮点型数组排序&输出乘法口诀表
1、目标
编写一个使用枚举和 S w i t c h − c a s e Switch - case Switch−case选择语句的程序
- 浮点型数组排序
- 编写1到9的乘法口诀表程序
2、枚举和Switch case
2.1 问题
枚举类型的构建和使用;通过 S w i t c h c a s e Switch\,\,case Switchcase语句来对符合条件值进行输出。
2.2 说明
编写一个Degrees的枚举,然后引用这个枚举。
2.3 分析
在此问题中首先需要定义一组不同类型的学位。为此,可通过以下方式创建枚举。
Enum Degrees
{
Bachelor, //学士
Master, //硕士
Doctor //博士
}
然后,可以使用switch结构,根据传递到此switch结构的枚举成员来决定要显示的信息。
2.4 参考步骤:
(1)为解决方案创建一个控制台应用程序的项目“Example_3”。
(2)将“Program.cs”类文件重命名为“EnumDemo.cs”。
(3)将以下代码添加到“EnumDemo.cs”中。
2.5 代码
using System;
using System.Collections.Generic;
using System.Text;
namespace Example_3
{
// 学位枚举列表
enum Degrees
{
// 枚举成员
Bachelor,
Master,
Doctor
}
/// <summary>
/// 此程序演示枚举和 switch case 的用法。
/// </summary>
class EnumDemo
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 用 switch case 来为相应的 case 显示信息。
switch (Degrees.Master)
{
case Degrees.Bachelor:
Console.WriteLine("你的学位为学士");
break;
case Degrees.Master:
Console.WriteLine("你的学位为硕士");
break;
case Degrees.Doctor:
Console.WriteLine("你的学位为博士");
break;
default: break;
}
Console.ReadLine();
Console.ReadKey();
}
}
}
2.6 输出结果
3、浮点型数组排序
3.1 问题
用户输入6个浮点型数组,程序根据其值的大小,进行排序输出。
说明
编写一个程序来接受用户输入的6个浮点数值,把这些数存放到一个数组里,然后对这些数组里面的值进行排序。
3.2 分析
此问题要求使用一个数组来接受和存储用户输入的6个值。该数组可通过以下方式声明:
// 声明长度为6的数组
float[] elements = new float[6];
用冒泡法对数组进行排序:需要使用一个for循环来接受用户输入的值。再用一个嵌套for循环比较数组中的元素。
3.3 参考步骤:
(1)为解决方案创建一个控制台应用程序的项目“Example_4”。
(2)将“Program.cs”类文件重命名为“ArrayDemo.cs”。
(3)将以下代码添加到“ArrayDemo.cs”中。
3.4 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace example2
{
class Program
{
static void Main(string[] args)
{// 声明长度为6的数组
float[] elements = new float[6];
// 计数器变量
int index;
// 临时变量
float temp;
Console.WriteLine("输入要进行排序的6个浮点数值:");
// For 循环接受用户输入的值
for (index = 0; index < elements.Length; index++)
{
elements[index] = float.Parse(Console.ReadLine());
}
Console.WriteLine("\n已排序的数组:");
// 嵌套for 循环对值进行比较
for (index = 0; index < elements.Length; index++)
{
for (int j = index + 1; j < elements.Length; j++)
{
// 如果值不以升序排序,就交换这些值
if (elements[index] > elements[j])
{
temp = elements[index];
elements[index] = elements[j];
elements[j] = temp;
}
}
Console.WriteLine(elements[index]);
}
Console.ReadKey();
}
}
}
3.5 Program.cs的输出结果
4、输出乘法口诀表
4.1 问题
编写程序输出从1到9的乘法口诀表。
4.2 说明
口诀表的前一部分如下:
11=1;
12=2;22=4;
13=3;23=6;33=9;
14=4;24=8;34=12;44=16;
4.3 分析
使用两个嵌套for循环来实现,父循环从1到9,子循环从1到父循环的当前值。
4.4 推荐步骤:
(1)建立一个控制台应用程序项目,命名为“multiplicationTable”。
(2)把以下代码添加到“Program.cs”中。
4.5 代码
using System;
using System.Collections.Generic;
using System.Text;
namespace multiplicationTable
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 10; i++)
{
//输出一行
for (int j = 1; j <= i; j++)
{
Console.Write(j.ToString()+" * "+i.ToString()+"; ");
}
Console.Write("\n");//换行
}
}
}
}
4.6 Program.cs的运行结果
5、练习
5.1 练习一
5.1.1 问题
设计项目s2-1。功能要求:输出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为 153 = 1 3 + 5 3 + 3 3 153=1^3+5^3+3^3 153=13+53+33。
5.1.2 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace s2_1
{
class narcissus
{
static void Main(string[] args)
{
Console.WriteLine("水仙花数:");
for (int i = 100; i < 999; i++)
{
int a = i / 100;
int b = i / 10 - a * 10;
int c = i % 10;
//输出一行
if (i == Math.Pow(a, 3) + Math.Pow(b, 3) + Math.Pow(c, 3))
{
Console.WriteLine(i.ToString());
}
}
}
}
}
5.2 练习二
5.2.1 问题
设计项目s2-2。功能要求:给定下列字符串s,分别统计出其中英文字母、空格、数字和其它字符的个数。
string s = @“1.One day, a father and his little son were going home. At this age, the boy was interested in all kinds of things and was always asking questions. 2.Now, he asked, ‘What’s the meaning of the word ‘Drunk’, dad?’ ‘Well, my son,’ his father replied, ‘look, there are standing two policemen. 3.If I regard the two policemen as four then I am drunk.’”;
5.2.2 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace s2_2
{
class count
{
static void Main(string[] args)
{
int letter = 0, space = 0, number = 0, others = 0;
string s = @"1.One day, a father and his little son were going home. At this age, the boy was interested in all kinds of things and was always asking questions. 2.Now, he asked, 'What's the meaning of the word 'Drunk', dad?' 'Well, my son,' his father replied, 'look, there are standing two policemen. 3.If I regard the two policemen as four then I am drunk.'";
foreach (char c in s) //提取数组中的整数
{
if (c >= 48 && c <= 57) number++;
else
{
if (c == ' ') space++;
else
{
if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
letter++;
else others++;
}
}
}
Console.WriteLine("统计结果:\nLetters:{0}\nSpaces:{1}\nNumbers:{2}\nOthers:{3}", letter, space, number, others);
}
}
}