我是靠谱客的博主 花痴酒窝,这篇文章主要介绍C#基础1:输入输出+变量定义,现在分享给大家,希望可以做个参考。

 

PS:注释和讲解全在代码中

1. 输出Hello world

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
//项目名,命名空间
{
class Program
//Program类
{
static void Main(string[] args)
//主函数
{
//Console:控制台
Console.WriteLine("Hello World!");
//WriteLine:输出一行(并换行),相当于printf("Hello World!n");
Console.WriteLine("two line");
Console.ReadKey();
//Readkey:字面意思233,等待用户输入一个字符,当然在这里没有什么意义就是了
}
}
}

 

2. 简单变量定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
{
class 简单变量定义
{
static void Main()
{
string thisIsName;
//驼峰命名法:变量名除第一个单词外,其余的每个单词的首字母大写。例如: time, playerName
thisIsName = "hautcds";
Console.WriteLine(thisIsName);
}
}
}

 

3. 简单变量运算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
{
class 简单变量运算
{
static void Main()
{
int p, q;
double k;
p = 5;
Console.WriteLine("p的值为:" + p++ + " " + p);
//输出结果为:p的值为:5 6
Console.Write("我想说:");
//输出text,但是不换行
Console.Write("其实C#的运算符及运算规则和C++是一致的n");
//相当于Console.WriteLine("……");
Console.Write("所以没什么可讲的n");
Console.WriteLine(5<4 && p==6);
//输出结果为False
p = 5; q = p*2; k = p/3.0;
Console.Write("p的值为{0},q的值为{1:N5},k的值为{2:N6}n", p, q, k);
//{2:N6}表示当前位置第2个变量四舍五入到6位小数
//输出结果为:p的值为5,q的值为10.00000,k的值为1.666667
//相当于C语言中的printf("p的值为%d,q的值为%d.00000,k的值为%.6fn", p, q, k);
//其中{}为占位符,{x}表示当前位置输出后面的第x+1个变量
//Console.WriteLine("{0}{1}", p);
左边有两个表示不同变量的占位符,可是右边只有一个变量,会报错!
Console.WriteLine("{0} {1} {0}", p, q);
//正确,输出5 10 5
}
}
}

 

4. 简单输入和类型转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C4_程序设计
{
class 简单输入
{
static void Main()
{
string name;
name = Console.ReadLine();
//读一行,返回string类型
Console.WriteLine("""+name+""");
//输出结果为:"name",在这里为转义符,和C++一样
Console.WriteLine(@"C:Program Files (x86)Dell Digital Delivery");
//字符串前面加个@,表示取消该字符串中的作用,使它成为普通字符,当然也可以使用\,不过这样比较麻烦,毕竟你要在每个""前面都加个
//Convert类型转换:
string s1 = "100", s2 = "31.115", temp;
int p = Convert.ToInt32(s1);
//int p2 = Convert.ToInt32(s2);
会报错,s2表示的数字是个小数,没法转换成整型,比较僵硬
double q = Convert.ToDouble(s2);
Console.WriteLine((int)(p+q));
//输出结果:131
temp = Convert.ToString(p+q);
Console.WriteLine(temp);
//输出结果:131.115
const double Pi = 3.1415926535897;
p = Convert.ToInt32(Console.ReadLine());
//读入一个整数,当然你要书写规范,不然会报错
//假设读入的p=1000000007
Console.WriteLine("{0:N3}", p*Pi);
//输出结果为3,141,592,675.581
Console.WriteLine(p*Pi);
//输出结果为3141592675.58085
Console.WriteLine("{0}", p*Pi);
//输出结果为3141592675.58085
//Pi = 52;
错误,常量不可修改
}
}
}

 

最后

以上就是花痴酒窝最近收集整理的关于C#基础1:输入输出+变量定义的全部内容,更多相关C#基础1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部