练习1:比较两个数大小(单分支)
int intNum = int.Parse(Console.ReadLine());
int intNum2 = int.Parse(Console.ReadLine)
if(intNum > intNum2)
{
Console.WriteLine ("a的值比b大,a的值为{0}",intNum);
}
练习2:比较三个数的大小
if(intNum1 > intNum2){
Console.WriteLine (intNum1>intNum3?intNum1:intNum3);
}else{
Console.WriteLine (intNum2>intNum3?intNum2:intNum3);
}
练习3:输入一个年份,判断该年份是否是闰年?如果是闰年,则输出该年是闰年,否则输出该年不是闰年
闰年:能被400整除或者(能被4整除,但不能被100整除)
int year = int.Parse(Console.ReadLine());
if(year % 400 ==0 || (year % 4 ==0 && year % 100 != 0 )){
Console.WriteLine ("该年是闰年");
}else{
Console.WriteLine ("该年不是闰年");
}
练习4:从控制台输入1~7,对应星期几打印出应该做什么
string s = Console.ReadLine();
switch (s) {
case "星期一":
Console.WriteLine ("吃饭");
break;
case "星期二":
Console.WriteLine ("睡觉");
break;
case "星期三":
Console.WriteLine ("学习");
break;
case "星期四":
Console.WriteLine ("运动");
break;
case "星期五":
Console.WriteLine ("聚会");
break;
case "星期六":
Console.WriteLine ("游戏");
break;
case "星期日":
Console.WriteLine ("休息");
break;
default:
Console.WriteLine ("好无聊哦");
break;
}
练习5:累加1-100的和
int sum = 0;
int intNum = 1;
while(intNum<=100){
sum += intNum;
intNum++;
}
Console.WriteLine (sum);
练习6:输出1-100之间的所有偶数和
int a = 1;
int intSum = 0;
while(a<=100){
if(a%2==0){
intSum +=a;
}
a++;
}
Console.WriteLine (intSum);
练习7:输出1-100之间的所有数字
int i = 1;
while(i<=100){
Console.WriteLine (i);
i++;
}
练习8:输出1-100之间所有能整除4的数
int a = 1;
while(a<=100){
if(a%4==0){
Console.WriteLine (a);
}
a++;
}
练习9:输出1-100之间所有的奇数和
int a =1 ;
int intSum = 0;
while(a<=100){
intSum += a;
if(a%2 !=0){
intSum +=a;
}
a++;
}
Console.WriteLine (intSum);
练习10:输出1~100之间所有能被三整除但是不能被4整除的所有奇数
int a = 1;
do{
if(a%2 !=0){
if(a%3 ==0 && a%4 !=0){
Console.WriteLine (a);
}
}
}while(a<=100);
网友评论