C# Learning Diary 04---Integer Type of Data Type
上一篇中我们初步的了解了一些数据类型,不全面,本着认真求实的精神,我再对数据类型梳理 一下。
值类型之整数类型:
记得以前上C语言 课 的时候老师也曾让我们看过这样的表格要求记下来,当时也没好好看认为记这个没什么用,可当我运行下面的代码的时候就知道老师的良苦用心了:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { class Program { static void Main(string[] args) { short x = 32766; x++; Console.WriteLine(x); x++; Console.WriteLine(x); Console.ReadKey(); } } }
结果出人所料:
为毛是负的????这个时候就章现了取值范围的伟大之处了 3268他超出了Short取值范围了;
与此类似的经历有很多,记得有一次老师叫我们写一个程序录入一个人的电话号码,然后输出,同桌说 “很简单嘛,三下五除二就写好了, ”代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { class Program { static void Main(string[] args) { int x; Console.WriteLine("姓名:"); String name = Console.ReadLine(); Console.WriteLine("电话号码: "); x = int.Parse(Console.ReadLine()); //类型转换 Console.WriteLine("你的名字叫:" + name + "\t" + "你的电话是:{0}", x); } } }
结果不能运行,原因就是int的有效位只有 10位 电话号码有11位 超出范围了,可以用long型 替代。再对输入与输出篇补充一点 : Console.WriteLine("你的名字叫:"+name) 中间的加号 + 表示的是连接2个字符串的意思,也就是合二为一的意思,当表达式中有一个String类型时 ,比如
int i =9; String Str = ”HC666“ Console.WriteLine(Str+i);
运行输出为:
HC6669
这是因为当表达式中有String类型与int类型时 编译器自动将int 型 转化为String类型(隐式转换)然后再连接起来。后面会学习隐式转换。
本着学无止境的精神,对上一篇日记的探索我有了新的发现: 定义变量名称时 用汉字也可以,并且不出错。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example { class Program { static void Main(string[] args) { String 姓名="HC666"; int 年龄=16; double 身高=1.70; Console.WriteLine("姓名:{0},年龄:{1},身高:{2}", 姓名, 年龄, 身高); } } }
其 实C#是支持多国语言编写的,不仅是中文,用日文、韩文……也都可以,不过建议还是用英文吧!!正好练习英语。
以上就是C#学习日记04---数据类型 之 整数类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Web Services in C#. Here we discuss an introduction to Web Services in C# with technology use, limitation, and examples.
