例题:
1-1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj1_1 { class Program { static void Main(string[] args) { int a, b, c; //定义变量 Console.Write("a:"); //输出屏幕提示信息 a = int.Parse(Console.ReadLine()); //从键盘获取字符串并转换成整数 Console.Write("b:"); b = int.Parse(Console.ReadLine()); c = a + b; //加法运算 Console.WriteLine("a+b={0}", c); //输出结果 } } }
编程题:
1.两个整型变量的定义为“int a = 1234,b = -1234;”,编写一个控制台项目输出他们的值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace exci1_1 { class Program { static void Main(string[] args) { int a = 1234,b=-1234; Console.WriteLine("a={0},b={1}",a,b); Console.WriteLine("a={0:D5},b={1:D5}", a, b); Console.WriteLine("a={0:c3},b={1:c2}", a, b); } } }
2.一个double变量的定义为“double d = 123.456;”,编写一个控制台项目输出他的值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace exci1_2 { class Program { static void Main(string[] args) { double d = 123.456; Console.WriteLine("{0,5:c}", d); Console.WriteLine("{0:E4}", d); Console.WriteLine("{0:f4}", d); Console.WriteLine("{0:g}", d); Console.WriteLine("{0:n}", d); } } }
ending