[Day4]C# 鸡础观念- 核心的数据成员~变数(二)

千变万化的字串变数/images/emoticon/emoticon07.gif

  • 他为什麽千变万化呢?因为字串这种型别,别人给他什麽他就是什麽
  • 就如同一位认真向学的小朋友,他如此天真无邪,老师教的都会认真记住
  • 在C#的世界中,我们要表明一个字串时都需要用""把他包起来

范例:我们来看看有""跟没有""的差异吧!

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个整数叫做 hi,然後赋予他100的值
            int hi = 100;

            //我们来列印看看
            Console.WriteLine("我没有加双引号 : " + hi);
            Console.WriteLine("我有加双引号 : " + "hi");
            Console.ReadKey();
        }
       
    }
}

结果:

我没有加双引号 : 100
我有加双引号 : hi

由此我们可以发现,加上""的"hi"他是被当成文字的,没有""的hi他则是整数变数,会直接印出他的值
我们接着开始介绍他们这型别的成员吧!

首先欢迎字元型变数~char/images/emoticon/emoticon44.gif

  • char他顾名思义只能存一个字元
  • 他跟大家不一样的地方在於,他使用的是单引号'',而不是双引号""

范例:使用一个char型别变数来印出A B C三个字母吧

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个char型别的变数
            char hi;

            //先赋值A给变数hi,然後印出来
            hi = 'A';
            Console.WriteLine("1. " + hi);

            //再来赋值B给变数hi,然後印出来
            hi = 'B';
            Console.WriteLine("2. " + hi);

            //最後赋值C给变数hi,然後印出来
            hi = 'C';
            Console.WriteLine("3. " + hi);

            Console.ReadKey();
        }
       
    }
 }

结果:

  1. A
  2. B
  3. C

再来是善变的字串型变数~string/images/emoticon/emoticon69.gif

  • string类型可以存多个字元
  • 我们常常会需要让用户输入一长串的资讯,或是我们需要显示一些提示给用户,他就是最佳人选

范例:我们让电脑自己跟自己来个尬聊

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个string型别的变数
            string hi;

            //让电脑来个问候
            hi = "安安,你好,几岁?住哪里?";
            Console.WriteLine(hi);

            //让电脑回答自己
            hi = "你好,你是个好人,但我才不要告诉你勒";
            Console.WriteLine(hi);

            Console.ReadKey();
        }
       
    }
}

结果:

安安,你好,几岁?住哪里?
你好,你是个好人,但我才不要告诉你勒

我们来玩转字串吧/images/emoticon/emoticon58.gif

1.字串的读取

  • 在这边我们会用到Console.ReadLine()这个函式
  • 我们来试着跟电脑对话吧

范例:我们来对电脑输入自己的名字让他回答

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //我们一开始使用Console.ReadLine()函式,让电脑听听我们的心声
            //并将输入的名字赋值到字串变数中
            string name = Console.ReadLine();

            //接下来让电脑回答我们
            Console.WriteLine(name + ",帅气十足!");

            Console.ReadKey();
        }
       
    }
}

输入:

孤独一只鸡

结果:

孤独一只鸡
孤独一只鸡,帅气十足!

2.字元的读取

  • 在这边我们会用到Console.Read()这个函式
  • 注意:Console.Read()读出来会是字元的ASCII,所以会是整数

范例:我们来对电脑输入一个字母,来查询他ASCII编码

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //我们使用Console.Read()函式,让电脑读取我们输入的字母
            //电脑会自动将他转成ASCII编码
            //并将ASCII编码赋值到变数中
            int ch = Console.Read();

            //印出输入字母的 ASCII
            Console.WriteLine("The ASCII code is " + ch);
            Console.ReadKey();
        }

    }
}

输入:

a

结果:

a
The ASCII code is 97

输入:

A

结果:

A
The ASCII code is 65

注:对於电脑而言,大写或小写甚至是任何符号,都有自己特有的编码所以我们这边输入大小写後会得到的结果

3.数字的读取

  • 在这边我们会用到Convert.ToInt32()这个函式
  • Convert.ToInt32()这函式的用途,就是将其他型别转成整数型别,称为"转型"
  • 就如同有人要移民来到美国,就必须到移民署办理移民,而且要符合一些相关的规定,不然是办理不成功的

范例:我们来对电脑输入半径,让他自动算出圆面积

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告半径为整数变数
            int r;
            //宣告圆面积为浮点数变数
            double s;
            //宣告圆周率为浮点数变数
            double π = 3.1415926536;

            //我们输入一个半径,并将输入的半径转成整数,赋值给变数r
            Console.Write("r = ");
            r = Convert.ToInt32(Console.ReadLine());
            
            //计算面积 面积 = 圆周率 X 半径 X 半径
            s = π * r * r;
            //印出答案
            Console.WriteLine("Ans:" + s);

            Console.ReadKey();
        }

    }
 }

输入:

5

结果:

r = 5
Ans:78.53981634

4.字串的累加

  • 当字串遇见了运算子"+",究竟会产生什麽样的爱恨纠葛呢?
  • 字串会直接衔接,并不会合体
  • 就如同我将隔壁邻居的地买下来,那块地还是在那个地方,并不会跟我的土地融合

范例:我们来产生各种口味的孤独动物吧

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

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告鸡
            string chicken = "鸡";
            //宣告鱼
            string fish = "鱼";
            //宣告狗
            string dog = "象";
            //宣告形容词
            string lonely = "孤独一只";

            //将他们做累加
            Console.WriteLine(lonely + chicken);
            Console.WriteLine(lonely + fish);
            Console.WriteLine(lonely + dog);

            Console.ReadKey();
        }

    }
}

结果:

孤独一只鸡
孤独一只鱼
孤独一只象

变数的命名规则/images/emoticon/emoticon50.gif

  • 最後在这边跟各位说明一下命名规则
  1. 变数第一个字必须是字母
  2. 底线或是@之後的字符可以是字母、数字或底线
  3. 变数不能和关键字重名

范例:

正确命名:

x
a1
myname
flowersCheckedListBox
_number
@float

错误命名:

8thStreet        //不能以数字开头命名
float               //不能和关键字同名

注意: C#区分大小写,比如name和Name是不同的变数,在为变数命名时,尽量选择有意义的名称。

关於变数的凄美爱情故事,我们就介绍到这边了,大家明天见!/images/emoticon/emoticon51.gif


<<:  [Day2] Android - Kotlin笔记: lazy

>>:  Day 05 : ML 专案生命周期

[Day4] Web 小花花

[Day4] Web 小花花 不要问我为啥我的标题这麽傻白甜 我也不知道,取名好难 路上看到可爱小植...

EP 9: Navigation by Shell in TopStore App

Hello, 各位 iT邦帮忙 的粉丝们大家好~~~ 本篇是 Re: 从零开始用 Xamarin 技...

[DAY 07] 牛墟婆婆肉粽蛋饼

牛墟婆婆肉粽蛋饼(其实他没有店名@@) 地点:盐水区土库路(朝琴路右转一下子就到) 时间:早上 刚到...

虹语岚访仲夏夜-20(专业的小四篇)

本来想好好的喝杯咖啡,算了算了...结果被Allen一气,我先到了会议室...等下才开始,还是看一下...

[Day14][笔记] React 事件处理

重点整理 事件命名上必须使用小驼峰,且为 String 事件中必须放一个回传 Function re...