您的位置:首页 > 编程语言 > C#

【C#基础】【语法03】课堂作业题 01-11

2014-08-26 18:09 771 查看
数组练习

1、定义一个int类型数组,长度为5,要求循环输入5个数给数组元素赋值,并输出第1个元素的值

2、假设有数组 int[] a=new int[]{1,2,3,4,5,6};

将a数组的下标0,1的元素交换

将a数组的下标2,3的元素交换

将a数组的下标4,5的元素交换

最后输出交换后的数组元素的值。(使用循环实现)

效果如下:最终a数组的元素为2,1,4,3,6,5

3、假设有数组 int[] a=new int[]{1,2,3,4,5,6};

将a数组的下标0,5的元素交换

将a数组的下标1,4的元素交换

将a数组的下标2,3的元素交换

最后输出交换后的数组元素的值。(使用循环实现)

效果如下:最终a数组的元素为6,5,4,3,2,1

4、定义一个int类型数组,长度为5,循环输入每个元素的值,再循环输出每个元素的值:

要求用for循环与foreach循环两种方式实现

5、定义一个int类型数组,长度为5,循环输入每个元素的值,计算数组中所有元素相加之和。

6、定义两个数组 int[] a=new int[]{1,2,3,4,5};

int[] b=new int[5];

通过循环将a数组的元素拷贝到b数组中,即b数组的值也是1,2,3,4,5

7、定义数组 int[] a=new int[5];

循环输入a数组元素的值

1)利用循环求出该数组中的最大值

2) 利用循环求出该数组中的最小值

方法练习

1、 输入长和宽,返回长方形面积,

面积GetArea(长,宽)

int GetArea(int w,int h)

2、输入1个字符串,返回字符串的长度

长度GetLength(字符串)

3、根据用户选择的类型,返回对应的价格

提示用户“请选择饮料: 小杯,中杯,大杯”,return得到文字对应的价格。

小杯—2元

中杯—5元

大杯—10元

价格 GetPrice(杯子的类型)

double GetPrice(string type)

4、输入n1,n2,计算n1到n2的累加和

例如:输入1和10,则求1到10的累加和

int GetSum(int n1,int n2)

数组练习

第一题:

namespace PB_1_循环输入5个数给数组元素赋值
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[5];
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("请输入第{0}个数组元素赋值:",i);
nums[i-1] = int.Parse(Console.ReadLine());
}
Console.WriteLine("数组中第一个元素的值为:"+nums[0]);
Console.ReadKey();
}
}
}


第二题:

namespace PB_2_交换数组元素并输出
{
class Program
{
static void Main(string[] args)
{
//引入交换中间元素
int change = 0;

int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
for (int i = 1; i <= 6; i+=2)
{
change = a[i-1];
a[i - 1] = a[i];
a[i] = change;
}
for (int j = 0; j <= 5; j++)
{
Console.Write(a[j]);
}
}
}
}


第三题:

namespace PB_3_交换数组元素使倒序输出
{
class Program
{
static void Main(string[] args)
{
//引入交换中间元素
int change = 0;

int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
int k = 5;
for (int i = 0; i <= 2; i++)
{

change = a[i];
a[i] = a[k];
a[k] = change;
k--;
}
foreach (var c in a)
{
Console.Write(c);
}
Console.ReadKey();
}
}
}


第四题:

namespace PB_4_分别用for和foreach循环输出数组每个元素的值
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[5];
for (int i = 1; i <6; i++)
{
Console.WriteLine("请输入第个{0}数组元素:",i);
nums[i-1]=int.Parse(Console.ReadLine());
}
Console.WriteLine();

//foreach输出
Console.WriteLine("foreach输出:");
foreach (var c in nums)
{
Console.Write(c);
}
Console.WriteLine();

//for循环输出
Console.WriteLine("for循环输出:");
for (int j = 0; j <= 5; j++)
{
Console.Write(nums[j]);
}
Console.ReadKey();
}
}
}


第五题:

namespace PB_5_循环输入元素并计算和
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[5];
for (int i = 1; i < 6; i++)
{
Console.WriteLine("请输入第个{0}数组元素:", i);
nums[i - 1] = int.Parse(Console.ReadLine());
}

int he = 0;
foreach (char c in nums)
{
he += c;
}
Console.WriteLine("数组元素之和为:{0}",he);
Console.ReadKey();
}
}
}


第六题:
namespace PB_6_拷贝a数组到b数组
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[5];

//拷贝前:
Console.WriteLine("拷贝前:");
Console.WriteLine("a组:");
foreach (int c in a)
{
Console.Write(c);
}
Console.WriteLine();
Console.WriteLine("b组:");
foreach (int c in b)
{
Console.Write(c);
}
Console.WriteLine();

//拷贝
for (int i = 1; i <= 5;i++ )
{
b[i - 1] = a[i - 1];
}

//拷贝后
Console.WriteLine("拷贝后:");
Console.WriteLine("a组:");
foreach (int c in a)
{
Console.Write(c);
}
Console.WriteLine();
Console.WriteLine("b组:");
foreach (int c in b)
{
Console.Write(c);
}
Console.WriteLine();
}
}
}


第七题:

namespace PB_7_求出数组最大最小值
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[5];
for (int i = 1; i < 6; i++)
{
Console.WriteLine("请输入第个{0}数组元素:", i);
a[i - 1] = int.Parse(Console.ReadLine());
}

int max = a[0];
int min = a[0];

//求最大值最小值
for (int i = 1; i <= 4;i++ )
{
if (a[i] > max)
{
max=a[i];
}
if (a[i] < min)
{
min = a[0];
}
}
Console.WriteLine("最大值为:" + max);
Console.WriteLine("最小值为:" + min);
}
}
}


方法练习

第一题:

namespace PB_01_方法练习_矩形面积
{
class Program
{
static void Main(string[] args)
{
juxing s = new juxing();

Console.WriteLine("请分别输入矩形长和宽:");
int w = int.Parse(Console.ReadLine());
int h = int.Parse(Console.ReadLine());
int mianji = s.GetArea(w,h);
Console.WriteLine("面积:" + mianji);

}

}
}


第二题:

namespace PB_02_输入字符串_返回其长度
{
class Program
{
static void Main(string[] args)
{
court wc = new court();
Console.WriteLine("Please enter a string:");
string chuan = Console.ReadLine();
int length = wc.GetLength(chuan);
Console.WriteLine("The length of the string you entered is:" + length);
}
}
}


第三题:

namespace PB_03_根据用户选择返回对应的价格
{
class Program
{
static void Main(string[] args)
{
price jia = new price();
Console.WriteLine("请选择饮料(小杯、中杯、大杯):");
string type = Console.ReadLine();
int price = jia.GetPrice(type);
Console.WriteLine("您选择的是{0},价格为{1}元。",type,price);
}
}
}


第四题:

namespace PB_04_计算n1到n2的累加和
{
class Program
{
static void Main(string[] args)
{
class1 he = new class1();
Console.WriteLine("请依次输入两个正整数:");
int n1 = int.Parse(Console.ReadLine());
int n2 = int.Parse(Console.ReadLine());
int sum1 = he.GetSum(n1, n2);
Console.WriteLine("两数之间的累加和为:" +sum1);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: