类在进行属性的封装后,能否在主函数中由用户输入数据??
类:using System;
using System.Collections.Generic;
using System.Text;
namespace class_student2
{
class student
{
private string sno;
public string _sno
{
set {sno = value; }
get { return sno; }
}
private string name;
public string _name
{
set { name = value; }
get { return name; }
}
private char sex;
public char _sex
{
set
{
if (value == '男' || value == '女')
sex = value;
else
Console.Write("请输入正确性别的格式!");
}
get { return sex; }
}
private int age;
public int _age
{
set{
if(value>=15&&value<=20)
age=value;
else
Console.Write("请输入正确的年龄!");
}
get{return age;}
}
private string email;
public string _email
{
set
{
if( value=="")
email = this.sno + "@
else
email = value + "@
}
get { return email;}
}
public void show()
{
Console.Write("学号:{0}\n姓名:{1}\n性别:{2}\n年龄:{3}\nE-mail:{4}",this.sno,this.name,this.sex,this.age,this.email);
}
}
}
主函数:
using System;
using System.Collections.Generic;
using System.Text;
namespace class_student2
{
class student2
{
static void Main(string[] args)
{
student st1=new student();
Console.Write("是否进行学生信息的输入y/n\n");
char r;
r= Convert.ToChar( Console.Read());
if (r == 'y')
{
Console.Write("请输入学号:");
st1._sno = Console.ReadLine();
Console.Write("请输入学生姓名:");
st1._name = Console.ReadLine();
Console.Write("请输入性别:");
st1._sex =Convert.ToChar( Console.Read());
Console.Write("请输入年龄:");
st1._age = Console.Read();
Console.Write("请输入E-mail:");
st1._email = Console.ReadLine();
Console.Write("信息输入完毕是否进行显示?y/n");
char f;
f = Convert.ToChar( Console.Read());
if (f == 'y')
st1.show();
else
Console.Write("感谢您的使用!");
Console.ReadLine();
}
else
{
Console.Write("感谢您的使用!");
Console.ReadLine();
}
}
}
}
各位大仙请问这程序如何修改才能通过啊。我再运行总是没有办法输入数据!!!!!