数组操作遇到的问题:
1、数组长度 = 数组下标+1; 数组下标 = 数组长度 -1;
2、如果定义数组为整形(int[])类型,就不能赋值字符串类型。
3.数组内存的是连续的、同一类型的数据
#region ======星光大道投票五个选手20人投票======
int[] xs = new int[5]; // 定义一个数组、有五个选手
//20人投票
for (int i = 0; i < 20; i++)
{
Console.WriteLine("您投几号:(1~5)");
int hao = Convert.ToInt32(Console.ReadLine());
xs[hao - 1]++;
}
//显示
for (int i = 0; i < xs.Length; i++)
{
Console.WriteLine("第{0}号选手的分{1}", i + 1, xs[i]);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ======教练给10球员打分,显示每位球员得分、显示最高分及球员,最低分及球员、显示平均得分======
int max = 0;//最高分
int min = 1000;//最低分
int[] qy = new int[10];//球员数组
//打分
for (int i = 0; i < qy.Length; i++)
{
Console.WriteLine("请给第{0}个球员打分", i + 1);
qy[i] = Convert.ToInt32(Console.ReadLine());
}
//显示每位球员得分
for (int i = 0; i < qy.Length; i++)
{
Console.WriteLine("第{0}个学员的分数为{1}", 1 + i, qy[i]);
}
//计算最高分、最低分
for (int i = 0; i < qy.Length; i++)
{
//最高分
if (qy[i] > max)
{
max = qy[i];
}
//最低分
if (qy[i] < min)
{
min = qy[i];
}
}
//求平均分
int sum = 0;
for (int i = 0; i < qy.Length; i++)
{
sum = sum + qy[i];
}
double avg = 1.0 * sum / 10;
Console.WriteLine("最高分为{0},最低分为{1},平均分{2}", max, min, avg);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ======青歌赛打分、20位评委给一个选手打分、去掉一个最高分、去掉一个最低分求平均得分、去掉两个分======
int[] pw = new int[10]; //20个评委
int xs_1 = 0;//选手得分
int max_1 = 0;
int min_1 = 0;
int sum_1 = 0;
double avg_1 = 0;
//评委打分
for (int i = 0; i < pw.Length; i++)
{
Console.WriteLine("请{0}号评委给选手打分:", i + 1);
pw[i] = Convert.ToInt32(Console.ReadLine());
}
//求最高分、最低分、平均分
min_1 = pw[0];
for (int i = 0; i < pw.Length; i++)
{
//最高分
if (pw[i] > max_1)
{
max_1 = pw[i];
}
//最低分
if (pw[i] < min_1)
{
min_1 = pw[i];
}
//求和
sum_1 = sum_1 + pw[i];
}
//求平均值
avg_1 = 1.0 * (sum_1 - max_1 - min_1) / 10;
//去掉两个最高分,去掉两个最低分,求平均分
//排序:
int z = 0; //中间量
for (int i = 0; i < pw.Length; i++)
{
for (int j = i; j < pw.Length - 1; j++)
{
if (pw[i] < pw[j + 1])
{
z = pw[i];
pw[i] = pw[j + 1];
pw[j + 1] = z;
}
}
}
double avg_2 = 1.0 * (sum_1 - pw[0] - pw[1] - pw[pw.Length - 1] - pw[pw.Length - 2]) / 10;
Console.WriteLine("去掉一个最高分{0},去掉一个最低分{1},平均得分为{2},去两个平均分为{3}", max_1, min_1, avg_1, avg_2);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ====== 老师20个学生语文成绩、及格、优秀、不及格、不及格显示学号======
int[] stu = new int[20];//20个学生成绩数组
int n = 0;//不及格的人数
for (int i = 0; i < stu.Length; i++)//接收学生成绩
{
Console.WriteLine("请输入第{0}个学员的成绩:", i);
int chengji = Convert.ToInt32(Console.ReadLine());
if (chengji < 60)
{
n++;
}
stu[i] = chengji;
}
//及格
for (int i = 0; i < stu.Length; i++)
{
// 不及格
if (stu[i] < 60)
{
Console.WriteLine("第{0}学员不及格;", i + 1);
}
}
for (int i = 0; i < stu.Length; i++)
{
//及格
if (stu[i] >= 60 && stu[i] < 80)
{
Console.WriteLine("第{0}学员及格;", i + 1);
}
}
for (int i = 0; i < stu.Length; i++)
{
//优秀
if (stu[i] > 80)
{
Console.WriteLine("第{0}学员优秀;", i + 1);
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ====== 球从10米落下,每次弹起80%,问第五次弹起的高度======
double gaodu = 10;
for (int i = 1; i <= 5; i++)
{
gaodu = gaodu * 0.8;
Console.WriteLine("第{0}次高度是{1}。", i, gaodu);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ====== 司机看到里程表的数字是12321,问下一次对称数字出现是什么数字======
int shu = 12322;
int gw = 0;
int sw = 0;
int bw = 0;
int qw = 0;
int ww = 0;
while (shu > 12321)
{
gw = shu % 10;
sw = shu / 10 % 10;
bw = shu / 100 % 10;
qw = shu / 1000 % 10;
ww = shu / 10000;
f (gw == ww && sw == qw)
{
Console.WriteLine("对称数为{0}", shu);
break;
}
shu++;
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion
#region ====== 存款利率是千分之三(年利率),每月会把利息加到本金上计算,存10000元 ,,第24个月存款多少======
//就是你存入银行的本金按利率计算的利息
//例如一年期银行利率为2.5%,就是你存入100元一年到期年得到100*2.5%=2.5元的利息
//例如半年期银行利率为2%,就是你存入100元半年到期时利息为100*2%/12个月*6个月=1元
double lx = 0;
double bj = 10000;
for (int i = 1; i <= 24; i++)
{
lx = bj * 0.3 / 12;
bj = lx + bj;
Console.WriteLine("第{0}个月的存款为{1},利息为{2}", i, bj, lx);
}
Console.WriteLine("第24个月的存款为{0}", bj);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
#endregion