- 使用 switch 語句判斷分?jǐn)?shù)區(qū)間
代碼
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("請(qǐng)輸入等級(jí): ");
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;
}
}
}
}
}
輸出
捕獲.PNG
- 角谷猜想
代碼
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("請(qǐng)輸入一個(gè)整數(shù): ");
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");
}
}
}
輸出
捕獲2.PNG
- 畫很多圓
代碼
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();
}
}
}
在設(shè)計(jì)界面只要添加一個(gè) button
輸出
捕獲.PNG