首页 > 代码库 > for循环练习
for循环练习
1.输入一个整数,计算从1加到这个数的结果
int sum = 0;
Console.WriteLine("请输入一个正整数");
int a = int.Parse(Console.ReadLine());
for(int i =0; i<=a ;i++)
{
sum += i;
}
Console.WriteLine("从1加到"+a+"的结果是:"+sum);
Console.ReadLine();
2.输入一个正整数n,求1-n的阶乘
3.案例:找出100以内与7有关的数并打印、
(1).从1找到100
(2).找出与7有关的数
个位数是7 a%10==7
十位数是7 a/10 ==7
能被7整除 a%7 ==0
4.//一个游戏,前20关是每一关自身的分数,
//21-30关每一关是10分
//31-40关,每一关是20分
//41-49关,每一关是30分
//50关,是100分
//输入你现在闯到的关卡数,求你现在拥有的分数
两种做法:if嵌套for for嵌套if
方法一:
方法二:
5. //100节楼梯,0-49节的每一节是本身的分数,
//从50开始是每一关10分。
//输入现在所到的节数,求现在的分数
6.//switch (变量名){case “”: break;}
//输入月份,日期号,输出是今年的第几天
//平年,2月28天
//1,3,5,7,8,10,12 31天之内
//4,6,9,11 30天以内
int m1 = 31, m2 = 29, m3 = 31, m4 = 30, m5 = 31, m6 = 30,
m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
Console.Write("输入月:");
int month = Convert.ToInt32(Console.ReadLine());
Console.Write("输入日:");
int day = Convert.ToInt32(Console.ReadLine());
switch (month)
{
case 1:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + day + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 2:
if (day >= 1 && day <= 29)
{
Console.WriteLine("第" + (day+m1) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 3:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day+m1+m2) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 4:
if (day >= 1 && day <= 30)
{
Console.WriteLine("第" + (day+m1+m2+m3) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 5:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day+m1+m2+m3+m4) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 6:
if (day >= 1 && day <= 30)
{
Console.WriteLine("第" + (day+m1+m2+m3+m4+m5) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 7:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day+m1+m2+m3+m4+m5+m6) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 8:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day+m1+m2+m3+m4+m5+m6+m7) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 9:
if (day >= 1 && day <= 30)
{
Console.WriteLine("第" + (day + m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 10:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day + m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 11:
if (day >= 1 && day <= 30)
{
Console.WriteLine("第" + (day + m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10) + "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
case 12:
if (day >= 1 && day <= 31)
{
Console.WriteLine("第" + (day+m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11)+ "天");
}
else
{
Console.WriteLine("输入有误!");
}
break;
default:
Console.WriteLine("输入有误!");
break;
}
Console.ReadLine();
for循环练习