C#一周学习

一个简易的账号管理系统

using System;
using System.IO;
using System.Reflection;

namespace AccountManagementSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isRunning = true;

            while (isRunning)
            {
                Console.WriteLine("账号管理系统");
                Console.WriteLine("请输入相应的数字进行操作:");
                Console.WriteLine("1. 注册新账号");
                Console.WriteLine("2. 登录账号");
                Console.WriteLine("3. 退出系统");

                string choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        RegisterAccount();
                        break;
                    case "2":
                        Login();
                        break;
                    case "3":
                        isRunning = false;
                        break;
                    default:
                        Console.WriteLine("无效的选择,请重新输入!");
                        break;
                }
            }

        }

        static void RegisterAccount()
        {
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();

            Console.WriteLine("请输入密码:");
            string password = Console.ReadLine();

            FileStream f = new FileStream("accounts.txt", FileMode.OpenOrCreate);
            f.Close();
            File.WriteAllText("accounts.txt", account+password);


            //将账号和密码写入文本文件
            using (StreamWriter writer = new StreamWriter("accounts.txt", true))
            {
                writer.WriteLine("{0}", account + password);
            }

            Console.WriteLine("账号注册成功!");
        }

        static void Login()
        {
         
            FileStream a = new FileStream("accounts.txt", FileMode.OpenOrCreate);
            
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();

            Console.WriteLine("请输入密码:");
            string password = Console.ReadLine();
            bool cunzai = false;
            StreamReader sr = new StreamReader(a);
            string inflie = sr.ReadLine();
            a.Close();
            if (inflie.Trim().Contains(account+password))
            {
                cunzai = true;
            }
            if (cunzai)
            {
                Console.WriteLine("登陆成功");
                main();
            }
            else
            {
                Console.WriteLine("账号或密码错误"); 
            }
        }

        static void main()
        {
            Console.WriteLine("1.查询鲜花数据2.储存鲜花数据3.删除鲜花数据4.更改鲜花数据5.退出系统");
            string num = Console.ReadLine();
            bool running = true;
            while (running)
            {
                switch (num)
                {
                    case "1": 
                        {
                            //chaxun();
                        }break;
                    case "2": 
                        {
                            chucun();
                        }break;
                    case "3": 
                        {
                            //sanchu();
                        }break;
                    case "4":
                        {
                            //gengai();
                        }break;
                    case "5":
                        {
                            running = false;
                        }break;
                    default :
                        {
                            Console.WriteLine("无效的选择请重新输入。");
                            num = Console.ReadLine();
                        }
                        break;
                }
            }     
        }

索引器

  1. 索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。

  2. 当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ]来访问该类的的成员。

namespace suoying
{
    class text
    {

        //定义一个简单的索引器
        public int this[int index]
        {
            get//当调用索引器时发生的事情
            {
                Console.WriteLine(index);
                return 100;//索引返回值
            }
            set//当赋值时所发生的事情
            {
                Console.WriteLine(index);
                Console.WriteLine(value);
            }
        }
    }
}
namespace suoying
{
    internal class Program
    {
        static void Main(string[] args)
        {

            text t = new text();
            t[2] = 200;
            Console.WriteLine(t[2]);
            Console.ReadLine();

        }
    }
}
输出:
2
200
2
100

索引器的简单使用

private string[] name = new string[10];
public string this[int index]
{
    get
    {
       return name[index];
    }
    set
    {
        name[index] = value;
    }
}
//
 text t = new text();
 t[0] = "小王";
 Console.WriteLine(t[0]);
 Console.ReadLine();
 输出:小王

利用索引器将星期转化为数字形式

namespace suoying
{
    internal class Week
    {
        private string[] days = { "Mon", "Tues", "Web", "Thurs", "Fri", "Sat", "Sun" };
        private int GetDay(string day)
        {
            int i = 0;
            foreach (string s in days)
            {
                if (s == day)
                {
                    return i + 1;

                }
                i++;
            }
            return -1;
        }
        public int this[string day]
        {
            get
            {
                return GetDay(day);
            }
        }

    }
}

Week week = new Week();
Console.WriteLine(week["Fri"]);
Console.Read();

输出:5
  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值