1.右击解决方案的引用,添加.NET中Microsoft.Office.Interop.Excel的引用;
2.在代码头添加using Microsoft.Office.Interop.Excel;
3.在函数中添加如下代码: //创建 Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application(); Workbook workbook1 = excel1.Workbooks.Add(true); Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"]; //写入 worksheet1.Cells[1, 1] = "航班号"; worksheet1.Cells[1, 2] = "起飞地点"; worksheet1.Cells[1, 3] = "降落地点"; //显示 excel1.Visible = true; Console.ReadLine(); //保存文件 workbook1.Close(true, "d:\\1.xls", null);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 验证手机号码的合法性
{
class Program
{
static void Main(string[] args)
{
bool areaAllow=false;//判断区号是否合法(区号138为合法)
Console.WriteLine("请输入手机号码:");
string PhoneNumber = Console.ReadLine();
string areaNumber = PhoneNumber.Substring(0, 3);//提取电话号码的前三位作为区号,提取子串,从0下标开始,提取3位
if (areaNumber == "138")
areaAllow = true;
else
Console.WriteLine("您好,您的手机号码为非法");
if (areaAllow == true)
{
if (PhoneNumber.Length == 11)
Console.WriteLine("您好,您的手机号码合法");
else
Console.WriteLine("您好,您的手机号码为非法");
}
}
}
}
测试完毕,可用。我觉得这种条件判断更容易理解,foreach语句一般是针对数组,而非字符串,至少在我现在了解的知识中foreach是这个效果
使用冒泡排序,参考代码:
int [] num = new int[4];
int i, j;
int temp;
Console.WriteLine("请输入4个数");
for (i = 0; i < 4; i++) {
Console.WriteLine("第{0}个数",(i+1));
num[i] = int.Parse(Console.ReadLine());
}
int max = num[0];
int min = num[0];
for (i = 0; i < num.Length - 1; i++) {
for (j = 0; j < num.Length - 1 - i; j++) {
if (num[j] > num[j + 1]) {
temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
Console.WriteLine("排序后的成绩为");
for (i = 0; i < num.Length; i++) {
Console.WriteLine("{0}\t",num[i]);
if (num[i] > max) {
max = num[i];
}
if (num[i] < min) {
min = num[i];
}
}
Console.WriteLine("最大值{0}\t最小值{1}",max,min);
Console.ReadLine();
Console.Readline是在运行的时候读取在控制台上输入的信息,当按回车的时候就读取输入的整行信息,使用的时候不需要带参数;Console.WriteLine是将信息显示到控制台,同时光标换行,使用的时候需要带一个参数,参数类型一般是一个字符串.
Console.Write("请输入一个年份:");
long x = Convert.ToInt64(Console.ReadLine());
if (x % 4 == 0)//当x可以被4整除时
{
if (x % 100 != 0)//当x不能被100整除时
{
Console.WriteLine("该年份为闰年");
}
else if (x % 400 == 0)//当x可以被400整除时
{
Console.WriteLine("该年份为闰年");
}
else//当x可以被100整除时
{
Console.WriteLine("该年份为非闰年");
}
}
else//当x不能被4整除时
{
Console.WriteLine("该年份为非闰年");
}
Console.ReadLine();
还没有评论,来说两句吧...