几个简单C#例子
2019-03-03 本文已影响0人
李药师_hablee
- 使用 switch 语句判断分数区间
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入等级: ");
char level = (char)Console.Read();
switch(char.ToUpper(level))
{
case 'A':
{
Console.WriteLine($"{level} 是90-100");
break;
}
case 'B':
{
Console.WriteLine($"{level} 是80-90");
break;
}
case 'C':
{
Console.WriteLine($"{level} 是60-80");
break;
}
case 'D':
{
Console.WriteLine($"{level} 是不及格");
break;
}
}
}
}
}
输出
- 角谷猜想
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个整数: ");
string n = Console.ReadLine();
int num = Convert.ToInt32(n);
int a = num;
int count = 0;
while(a!=1)
{
Console.Write($"{a}\t");
count++;
if(count%5==0)
{
Console.WriteLine();
}
if(a%2==1)
{
a = 3 * a + 1;
}
else
{
a = a / 2;
}
}
Console.Write($"{a}\t");
}
}
}
输出
- 画很多圆
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random random = new Random();
Color getRandomColor()
{
return Color.FromArgb(
random.Next(256),
random.Next(256),
random.Next(256));
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
int x0 = this.Width / 2;
int y0 = this.Height / 2;
for(int r=0;r<y0/2;r++)
{
g.DrawEllipse(new Pen(getRandomColor(), 1),
x0 - r, y0 - r, r * 2, r * 2);
}
g.Dispose();
}
}
}
在设计界面只要添加一个 button