C#_switch语句,for循环,dowhile循环,while循环1:switch语句,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication33
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输⼊分数:");
int score = int.Parse(Console.ReadLine());
Console.WriteLine("得分是:");
switch (score / 10)
{
case 10:
case 9:
Console.WriteLine("A");
break;
case 8:
Console.WriteLine("B");
break;
case 7:
Console.WriteLine("C");
break;
case 6:
Console.WriteLine("D");
break;
default:
Console.WriteLine("E");
break;
}
}
}
}
 运⾏结果:
2:for循环,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication34
{
class Program
{
static void Main(string[] args)
{
int i;
int sum = 0;
for (i = 0; i <= 100; i++)
{
sum += i;
}
Console.WriteLine("最终结果是:{0}",sum);
}
}
}
 运算结果:
3:do while语句,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication35
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int sum = 0;
do
{
sum += i;
i++;
} while (i <= 100);
Console.WriteLine("最终结果是:{0}", sum);
}
}
}
  运⾏结果:
4:while循环,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication36
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int sum = 0;
while(i<=100)
{
sum += i;
i++;
}
Console.WriteLine("最终结果是:{0}", sum);
switch语句c语言例子}
}
}
  运⾏结果⼀样。
5:下⾯来⼀个综合的例⼦,为多名职员计算缴税⾦额。代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication37
{
class Program
{
static void Main(string[] args)
{
decimal sal;
decimal salary;
decimal tax;
int count = 0;//记录处理数据个数
char ch;//记录⽤户输⼊的字符
do
{
Console.Write(++count);
Console.Write("请输⼊职员⼯资:");
salary = Convert.ToDecimal(Console.ReadLine());//输⼊职员⼯资
sal = salary - 3500;
if (sal <= 0m)
tax = 0m;//decimal类型要加后缀m
else if (sal <= 1500m)
tax = sal * 0.03m;
else if (sal <= 4500m)
tax = sal * 0.1m-105;
else if (sal <= 9000m)
tax = sal * 0.2m-555;
else if (sal <= 35000m)
tax = sal * 0.25m-1005;
else if (sal <= 55000m)
tax = sal * 0.3m-2755;
else if (sal <= 80000m)
tax = sal * 0.35m-5505;
else
tax = sal * 0.45m-13505;
Console.WriteLine("应交税⾦为:" + tax.ToString());
Console.Write("继续吗?");
ch = Convert.ToChar(Console.ReadLine());//⽤户输⼊字符
//若⽤户输⼊的是除⼤⼩写Y或⼤⼩写N以外的其他字符,则要求⽤户重新输⼊                while (ch != 'Y' && ch != 'y' && ch !='N' && ch != 'n')
{
Console.WriteLine("对不起!只允许⼤⼩写Y或⼤⼩写N!\n继续吗?");
ch = Convert.ToChar(Console.ReadLine());
}
}
while (ch == 'Y'||ch == 'y');
Console.WriteLine("谢谢使⽤!");
Console.ReadLine();
}
}
}
  运⾏结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。