C#求解
为什么我做出来只能保存一次,不能永久保存,只能用数组using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public int[] a = new int[100];
public string[] b = new string[100];
int r = 0;
public void tianjia()
{
Console.WriteLine("请输入你添加的用户名");
b[r] =Console.ReadLine();
Console.WriteLine("请输入你的密码");
a[r] = Convert.ToInt32(Console.ReadLine());
r++;
}
public void chazhao()
{
Console.WriteLine("请输入你要查找的用户名");
string j = Console.ReadLine();
Console.WriteLine("请输入密码:");
int mi = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < r; i++)
{
if (j == b[i] && mi == a[i])
{
Console.WriteLine("找到用户:用户名{0}密码{1}", b[i], a[i]);
}
else
{
Console.WriteLine("你输入的用户名或密码错误");
}
}
}
public void xiugai()
{
Console.WriteLine("请输入你要修改的用户名");
string xg = Console.ReadLine();
Console.WriteLine("请输入密码:");
int xm = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <r; i++)
{
if (xg == b[i] && xm == a[i])
{
Console.WriteLine("请输入新的用户名:");
string xin = Console.ReadLine();
Console.WriteLine("请输入新的密码");
int xi = Convert.ToInt32(Console.ReadLine());
b[i] = xin;
a[i] = xi;
}
}
}
public void xiansi()
{
for (int c = 0; c < r; c++)
{
Console.WriteLine("用户名:{0} 密码{1}", b[c], a[c]);
}
}
static void Main(string[] args)
{
Program sl = new Program();
Console.WriteLine("1是添加用户;2是查找用户;3是修改用户");
int mn = Convert.ToInt32(Console.ReadLine());
switch (mn)
{
case 1:
sl.tianjia();
break;
case 2:
sl.chazhao();
break;
case 3:
sl.xiugai();
break;
}
sl.xiansi();
}
}
}
--------------------编程问答-------------------- 你是要有记住密码这个功能吗? --------------------编程问答-------------------- 你应该将数据存在在数据中中,读取时再调用,而不是项目的变量中保存,只是在项目运行期间有效,关闭后,都不会保存. --------------------编程问答-------------------- 你数据需要持久化~ --------------------编程问答-------------------- 用户名、密码要保存在物理文件中,比如数据库、文本文件中 --------------------编程问答-------------------- 只为学习 --------------------编程问答-------------------- 我刚才问了下我们老师,用IO就行了,谢谢各位了 --------------------编程问答-------------------- 怎么保存呀 --------------------编程问答-------------------- FileStream --------------------编程问答-------------------- APP.SETTING --------------------编程问答-------------------- 求大神详细点,我试了一下数组保存不起呀 --------------------编程问答--------------------
--------------------编程问答-------------------- 以上是个比较简单的Demo...你只观注以下两个方法就好了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
namespace 将数组转成xml读取
{
class Program
{
static void Main(string[] args)
{
Person[] o = Init().ToArray();
///
/// 此处可理解成:
/// 要转化的对象是什么类型,以转化格式的时候按此类型格式进行转化
Type type = o.GetType();
//string a = type.FullName;
string path = Directory.GetCurrentDirectory()+@"\test.xml";
WriteXmlSerializer(type, o,path);
ReadeXmlSerializer(type,path);
}
/// <summary>
/// 读取XML,并将其转化成数组
/// </summary>
/// <param name="type"></param>
private static void ReadeXmlSerializer(Type type,string path)
{
string temp = "";
Person[] o = new Person[10];
//模拟读取数据库数据
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
temp = sr.ReadToEnd();
//将xml转成object[]
XmlSerializer xml = new XmlSerializer(type);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(temp)))
{
try
{
o = (Person[])xml.Deserialize(ms);
}
catch (Exception)
{
throw;
}
}
}
/// <summary>
/// 将数组转化成XML,
/// </summary>
/// <param name="type"></param>
/// <param name="o"></param>
private static void WriteXmlSerializer(Type type, Person[] o, string path)
{
string temp = "";
DataContractSerializer dcs = new DataContractSerializer(type);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(ms))
{
dcs.WriteObject(xw,o);
}
ms.Position = 0;
temp = System.Xml.Linq.XElement.Load(ms).ToString();
}
}
/// <summary>
/// 初使化学生集合
/// </summary>
/// <returns></returns>
private static List<Person> Init()
{
List<Person> students = new List<Person>();
Student s0 = new Student { id = 1, name = "张三", 易做图 = false, age = 12, address = "aaaaa" };
Student s1 = new Student { id = 2, name = "李四", 易做图 = true, age = 22, address = "bbbbbb" };
Student s2 = new Student { id = 8, name = "王五", 易做图 = false, age = 16, address = "cccc" };
Student s3 = new Student { id = 14, name = "赵六", 易做图 = true, age = 14, address = "ddddd" };
Student s4 = new Student { id = 6, name = "二麻子", 易做图 = true, age = 32, address = "eeeee" };
Student s5 = new Student { id = 3, name = "w", 易做图 = false, age = 26, address = "gggggg" };
Student s6 = new Student { id = 10, name = "fgg", 易做图 = false, age = 22, address = "jjjjj" };
Student s7 = new Student { id = 4, name = "qq", 易做图 = true, age = 23, address = "wwwww" };
Teacher t1 = new Teacher() { Id = 1, Name = "aaa", Sex = false };
Teacher t2 = new Teacher() { Id = 2, Name = "bbb", Sex = true };
Teacher t3 = new Teacher() { Id = 3, Name = "ccc", Sex = true };
students.Add(s0);
students.Add(s1);
students.Add(s2);
students.Add(s3);
students.Add(s4);
students.Add(s5);
students.Add(s6);
students.Add(s7);
students.Add(t1);
students.Add(t2);
students.Add(t3);
return students;
}
WriteXmlSerializer(type, o,path);
ReadeXmlSerializer(type,path);
实体也不要弄那么多个, 多类个继承 时 还要处理其它 东西,所以你只需要 写一个实体测试类就好了
补充:.NET技术 , C#