C#-基本語(yǔ)法應(yīng)用編程

1鸽粉、從一個(gè)數(shù)組中選擇最大最小值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請(qǐng)輸入5個(gè)數(shù)字:");
            int[] arr = new int[5];
            int max = int.MinValue;
            int min = int.MaxValue;
            for (int i = 0; i < 5; ++i)
            {
                arr[i] = Convert.ToInt16(Console.ReadLine());
                if (max < arr[i])
                    max = arr[i];
                if (min > arr[i])
                    min = arr[i];
            }

            Console.WriteLine("最大的數(shù)是:" + max);
            Console.WriteLine("最小的數(shù)是:" + min);
            Console.ReadKey();

        }
    }
}
image.png

2、九九乘法表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 10; ++i)
            {
                for (int j = 1; j <= i; ++j)
                {
                    if (i * j > 9)
                        Console.Write("" + i + " * " + j + " = " + i * j + "  ");
                    else
                        Console.Write("" + i + " * " + j + " = " + i * j + "   ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
image.png

3抓艳、分別使用for循環(huán)和while循環(huán)設(shè)計(jì)一個(gè)程序触机,計(jì)算從1加到100的和

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)
        {

            int sum = 0;
            for (int i = 1; i <= 100; ++i)
            {
                sum += i;
            }
            Console.WriteLine("for:1+..+100 = " + sum);
            sum = 0;
            int j = 1;
            while (j <= 100)
            {
                sum += j;
                j++;
            }
            Console.WriteLine("while:1+..+100 = " + sum);
            Console.ReadKey();

        }
    }
}
image.png

4、利用數(shù)學(xué)類提供的平方根方法計(jì)算并輸出1.0,2.0儡首,3.0片任,…,10.0的平方根蔬胯。

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)
        {
            for (int i = 1; i <= 10; ++i)
            {
                Console.WriteLine(i + "的平方根" + " = " + Math.Sqrt(i));
            }
            Console.ReadKey();

        }
    }
}
image.png

5对供、隨機(jī)數(shù)方法產(chǎn)生5個(gè)1~10(包括1和10)之間的整數(shù)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            Random ran = new Random();
            for (int i = 1; i <= 5; ++i)
            {
                Console.WriteLine("第" + i + "個(gè)隨機(jī)數(shù)是 " + ran.Next(1, 11));
            }
            Console.ReadKey();
        }
    }
}
image.png

6氛濒、編程實(shí)現(xiàn):隨機(jī)產(chǎn)生1~20之間的整數(shù)产场,總共生成1000次,統(tǒng)計(jì)其中生成的整數(shù)0舞竿,1京景,2,3骗奖,... …确徙,20的個(gè)數(shù)分別是多少,并輸出統(tǒng)計(jì)結(jié)果(每5個(gè)數(shù)一行)重归。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            Random ran = new Random();
            int[] arr = new int[1001];
            for (int i = 1; i <= 1000; ++i)
            {
                arr[ran.Next(1, 21)]++;
            }
            for (int i = 1; i < 10; ++i)
            {
                Console.WriteLine(i + "  出現(xiàn)的次數(shù)是: " + arr[i]);
            }
            for (int i = 10; i <= 20; ++i)
            {
                Console.WriteLine(i + " 出現(xiàn)的次數(shù)是: " + arr[i]);
            }
            Console.ReadKey();

        }
    }
}
image.png

7米愿、統(tǒng)計(jì)從鍵盤中輸入字符串中分別有多少個(gè)數(shù)字和字母。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            int letter = 0, digit = 0;
            string s1 = Console.ReadLine();
            foreach (char c in s1)
            {
                if (char.IsLetter(c))
                {
                    letter++;
                    continue;
                }
                if (char.IsDigit(c))
                {
                    digit++;
                    continue;
                }
            }
            Console.WriteLine("字母出現(xiàn)的次數(shù)是: " + letter);
            Console.WriteLine("數(shù)字出現(xiàn)的次數(shù)是: " + digit);
            Console.ReadKey();

        }
    }
}
image.png

8鼻吮、編寫程序,要求用戶輸入月份號(hào)碼较鼓,然后顯示該月的英文名稱椎木。例如,如果用戶輸入2博烂,程序應(yīng)顯示February香椎。要求月的英文名稱存于數(shù)組中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = new string[12];
            s[0] = "January"; s[1] = "February"; s[2] = "March"; s[3] = "April";
            s[4] = "May"; s[5] = "June"; s[6] = "July"; s[7] = "August";
            s[8] = "September"; s[9] = "October"; s[10] = "November"; s[11] = "December";
            int i = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine(i + "月份" + "是 " + s[i - 1]);
            Console.ReadKey();
        }
    }
}
image.png

9禽篱、請(qǐng)輸入一個(gè)代表項(xiàng)數(shù)的正整數(shù)N(N ≤ 100)畜伐,然后輸出1-3+5-7+9-11+……前N項(xiàng)的和。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = Convert.ToInt16(Console.ReadLine());
            int sum = 0;
            int num = 1;
            for (int j = 1; j <= i; ++j, num += 2)
            {
                if (j % 2 == 0)
                    sum -= num;
                else
                    sum += num;
            }
            Console.WriteLine("前" + i + "項(xiàng)和是 " + sum);
            Console.ReadKey();
        }
    }
}
image.png

10躺率、請(qǐng)編寫兩個(gè)程序玛界,分別通過(guò)if語(yǔ)句和switch語(yǔ)句兩種方式完成,輸入一個(gè)成績(jī)悼吱,將百分制成績(jī)轉(zhuǎn)換成等級(jí)制成績(jī)(A為100~90 ,…, F為不及格)慎框。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = Convert.ToInt16(Console.ReadLine());
            while (i < 0 || i > 100)
            {
                Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
                i = Convert.ToInt16(Console.ReadLine());
            }
            char a = 'F';
            if (i >= 90)
                a = 'A';
            else if (i >= 80)
                a = 'B';
            else if (i >= 70)
                a = 'C';
            else if (i >= 60)
                a = 'D';
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}
image.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = Convert.ToInt16(Console.ReadLine());
            while (i < 0 || i > 100)
            {
                Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
                i = Convert.ToInt16(Console.ReadLine());
            }
            char a = 'F';
            switch (i / 10)
            {
                case 10:
                case 9: a = 'A'; break;
                case 8: a = 'B'; break;
                case 7: a = 'C'; break;
                case 6: a = 'D'; break;
            }

            Console.WriteLine(a);
            Console.ReadKey();

        }
    }
}
image.png

11、讀入兩個(gè)正整數(shù)m和n后添,輸出m和n的最小公倍數(shù)笨枯。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = Convert.ToInt16(Console.ReadLine());
            while (n1 <= 0)
            {
                Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
                n1 = Convert.ToInt16(Console.ReadLine());
            }
            int n2 = Convert.ToInt16(Console.ReadLine());
            while (n2 < 0)
            {
                Console.WriteLine("數(shù)錯(cuò)了,再輸一遍");
                n2 = Convert.ToInt16(Console.ReadLine());
            }
            int min = Math.Min(n1, n2);
            for (int i = Math.Max(n1, n2); i < int.MaxValue; i++)
            {
                if (i % n1 == 0 && i % n2 == 0)
                {
                    Console.WriteLine("最小公倍數(shù)是 " + i); Console.ReadKey();
                    return;
                }
            }
            Console.WriteLine("沒(méi)找到最小公倍數(shù)");
            Console.ReadKey();

        }
    }
}
image.png

12、判斷一個(gè)整數(shù)n是否為素?cái)?shù)。若是則輸出:n是素?cái)?shù)馅精;否則輸出n不是素?cái)?shù)(注:n是具體的值)严嗜。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt16(Console.ReadLine());
            while (n < 0)
            {
                Console.WriteLine("請(qǐng)輸入正整數(shù),再輸一遍");
                n = Convert.ToInt16(Console.ReadLine());
            }
            if (n == 0 || n == 1)
            {
                Console.WriteLine(n + "不是素?cái)?shù)"); Console.ReadKey();
                return;
            }
            int MAX = (int)Math.Sqrt(n);
            for (int i = 2; i <= MAX; ++i)
            {
                if (n % i == 0)
                {
                    Console.WriteLine(n + "不是素?cái)?shù)"); Console.ReadKey();
                    return;
                }
            }
            Console.WriteLine(n + "是素?cái)?shù)!");
            Console.ReadKey();
        }
    }
}
image.png

13、用for循環(huán)計(jì)算s=1!+2!+3!+…+n!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請(qǐng)輸入一個(gè)正整數(shù):");
            int n = Convert.ToInt16(Console.ReadLine());
            while (n < 0)
            {
                Console.WriteLine("請(qǐng)輸入正整數(shù),再輸一遍");
                n = Convert.ToInt16(Console.ReadLine());
            }
            int sum = 0;
            int j = 1;
            for (int i = 1; i <= n; ++i)
            {
                j = j * i;
                sum += j;
            }
            Console.WriteLine("s=1!+2!+3!+…+n!的結(jié)果為:"+sum);
            Console.ReadKey();

        }
    }
}
image.png

14洲敢、
image.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 0;
            for (int i = 2; i <= 20; ++i)
            {
                d += Math.Pow(i, i);
            }
            if (double.MaxValue - 0.01 < d)
                Console.WriteLine("越界!!");
            Console.WriteLine(d);
            Console.ReadKey();
        }
    }
}
image.png

15漫玄、編寫一個(gè)程序,打印出以下圖形:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末沦疾,一起剝皮案震驚了整個(gè)濱河市称近,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌哮塞,老刑警劉巖刨秆,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異忆畅,居然都是意外死亡衡未,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門家凯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)缓醋,“玉大人,你說(shuō)我怎么就攤上這事绊诲∷土唬” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵掂之,是天一觀的道長(zhǎng)抗俄。 經(jīng)常有香客問(wèn)我,道長(zhǎng)世舰,這世上最難降的妖魔是什么动雹? 我笑而不...
    開(kāi)封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮跟压,結(jié)果婚禮上胰蝠,老公的妹妹穿的比我還像新娘。我一直安慰自己震蒋,他們只是感情好茸塞,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著喷好,像睡著了一般翔横。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上梗搅,一...
    開(kāi)封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天禾唁,我揣著相機(jī)與錄音效览,去河邊找鬼。 笑死荡短,一個(gè)胖子當(dāng)著我的面吹牛丐枉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掘托,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼瘦锹,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了闪盔?” 一聲冷哼從身側(cè)響起弯院,我...
    開(kāi)封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎泪掀,沒(méi)想到半個(gè)月后听绳,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡异赫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年椅挣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片塔拳。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鼠证,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出靠抑,到底是詐尸還是另有隱情量九,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布颂碧,位于F島的核電站娩鹉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏稚伍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一戚宦、第九天 我趴在偏房一處隱蔽的房頂上張望个曙。 院中可真熱鬧,春花似錦受楼、人聲如沸垦搬。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)猴贰。三九已至,卻和暖如春河狐,著一層夾襖步出監(jiān)牢的瞬間米绕,已是汗流浹背瑟捣。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留栅干,地道東北人迈套。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像碱鳞,于是被迫代替她去往敵國(guó)和親桑李。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 有時(shí)候生活就是這樣 禁荒,時(shí)而狂躁時(shí)而安靜,時(shí)而抑郁不堪總在細(xì)究生命的意義允华,又何苦這樣折磨自己呢圈浇?其實(shí),我也弄不懂靴寂,有...
    漾佈閱讀 411評(píng)論 0 0
  • 有人說(shuō) 愛(ài)一個(gè)人就要把相思放下 讓心與魂合一 沒(méi)有相思
    滄海一粟貝閱讀 132評(píng)論 1 4