1. int a=1234,b=-1234;
输出:
a=1234,b=-1234
a=01234,b=-01234
a=¥1,234,000 , b=¥-1,234,000
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 d=123.456;
输出:
¥123.46
1.2346E+002
123.4560
123.456
123.46
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);
3.声明一个学生结构类型Stud,包含学号、姓名和出生日期成员;定义Stud结构的两个学生s1,s2并赋值,求他们出生在星期几以及他们的出生相差的天数
enum WeekDay {星期日,星期一,星期二,星期三,星期四,星期五,星期六};
class Program
{
public struct Stud
{
public int num;
public string name;
public DateTime brithday;
}
static void Main(string[] args)
{
//思路1
Stud s1 = new Stud();
Stud s2 = new Stud();
s1.name = "李明";
s1.num = 1;
s1.brithday = new DateTime(1985, 10, 18);
s2.name = "王丽";
s2.num = 2;
s2.brithday = new DateTime(1986, 2, 16);
Console.WriteLine(s1.name + "的生日为:" + s1.brithday);
Console.WriteLine(s2.name + "的生日为:" + s2.brithday);
Console.WriteLine(s1.name + "出生在" + s1.brithday.DayOfWeek);
Console.WriteLine(s2.name + "出生在" + s2.brithday.DayOfWeek);
TimeSpan res = s2.brithday - s1.brithday;
Console.WriteLine(s1.name + "和" + s2.name + "相差" + res + "天");
//正确答案
Console.WriteLine(s1.name + "的生日为:{0}-{1}-{2}", s1.brithday.Year, s1.brithday.Month, s1.brithday.Day);
Console.WriteLine(s2.name + "的生日为:{0}-{1}-{2}", s2.brithday.Year, s2.brithday.Month, s2.brithday.Day);
int a = (int)s1.brithday.DayOfWeek;
int b = (int)s2.brithday.DayOfWeek;
Console.WriteLine(s1.name + "出生在{0}",(WeekDay)a);
Console.WriteLine(s2.name + "出生在{0}", (WeekDay)b);
Console.WriteLine(s1.name + "和" + s2.name + "相差" + res + "天");
Console.ReadKey();
}
输出结果:
李明的生日为1985-10-18
王丽的生日为1986-2-16
李明出生在星期五
王丽出生在星期日
李明和王丽相差121.00:00:00天
4.简述C#中continue语句的作用?
continue语句也用于循环语句,它类似于break,但它不是结束循环,而是结束循环语句的当前循环,接着执行下次一循环。
在while和do…while循环结构中,执行控制权转至对“条件表达式”的判断,在for结构中,转去执行“表达式2”。
5.简述C#中break语句的作用?
break语句使程序从当前循环(do…while和for)内跳转出来,接着执行循环语句后面的语句
6.读入一组整数(以输入0结束),分别输出其中奇数和偶数的和。
string a = Console.ReadLine();
string[] num = a.Split(' ');
int[] numArray = new int[num.Length];
for (int i = 0; i < num.Length; i++)
{
int temp = Convert.ToInt32(num[i]);
numArray[i] = temp;
}
int ji = 0, ou = 0;
do
{
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] % 2 == 1)
{
ji += numArray[i];
}
else
{
ou += numArray[i];
}
}
} while (numArray[numArray.Length-1] != 0);
Console.WriteLine("奇数之和={0}", ji);
Console.WriteLine("偶数之和={0}", ou);
7.输入正整数n,计算s=1+(1+2)+(1+2+3)+……+(1+2+3+……+n)。
int n = Convert.ToInt32(Console.ReadLine());
int s=0;
for (int i = 1; i <=n; i++)
{
for (int j = 1; j <= i; j++)
{
s = s + j;
}
}
Console.WriteLine(s);
8.求1+2!+3!+4!+……+n!的和。
int n = Convert.ToInt32(Console.ReadLine());
int i = 1, sum = 0, x = 1;
while (i <= n)
{
x = x * i;
i++;
sum += x;
}
Console.WriteLine(sum);
9.使一个负数-123翻转输出,仍然是负数。
static void Main(string[] args)
{
int a = -123;
Console.WriteLine(Reverse(a));
Console.ReadKey();
}
public static int Reverse(int x)
{
if (x < 0)
{
int.TryParse(new string((-x).ToString().Reverse().ToArray()), out x);
return -x;
}
else
{
int.TryParse(new string((x).ToString().Reverse().ToArray()), out x);
return x;
}
}
10.用递归法求5!。
static void Main(string[] args)
{
//int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Back(5));
Console.ReadKey();
}
static int Back(int num)
{
if (num == 1) return 1;
return num * Back(num - 1);
}
11.找出100到999之间的水仙花数
例:153=1*1*1+5*5*5+3*3*3
int ge, shi, bai, num = 100;
for (int i = 100; i < 1000; i++)
{
ge = i % 10;
shi = (i / 10) % 10;
bai = i / 100;
num = ge * ge * ge + shi * shi * shi + bai * bai * bai;
if (i==num)
{
Console.WriteLine(i);
}
}