幾個(gè)簡單C#例子

  • 使用 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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,336評(píng)論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,405評(píng)論 4 61
  • 這周的隨筆很奇怪,兩個(gè)題目選。反正寫都寫了,順便也提一下考完試之后的感想吧,我考完試之后最希望的就是……快點(diǎn)上新課...
    一蓑煙雨任平生0801閱讀 249評(píng)論 5 0
  • 之前在知乎上回答過一些介紹MacOS下編程心得的一些文章,這里匯總一下,希望幫到更多的朋友:軟件&工具人與動(dòng)物最大...
    51reboot閱讀 567評(píng)論 1 1
  • 人有三急,最急的時(shí)候如果沒有人救,人會(huì)容易恨這個(gè)人
    547426閱讀 117評(píng)論 0 2

友情鏈接更多精彩內(nèi)容