although there are still problems, try
using System;
struct PhoneBook
{
public string name;
public byte age;
public byte phone;
}class test
{
static void Main()
{
PhoneBook pl = new PhoneBook();
Console.WriteLine("Please Enter Your Name:");
pl.name=Console.ReadLine();
Console.WriteLine("Please Enter Your Age:");
string s = Console.ReadLine();
pl.age= Convert.ToByte(s);
Console.WriteLine("Please Enter Your Phone:");
s = Console.ReadLine();
pl.phone=Convert.ToByte(s);
Console.WriteLine("{0}{1}{2}",pl.name,pl.age,pl.phone);
}
}
using System;
struct PhoneBook
{
public string name;
public byte age;
public byte phone;
}class test
{
static void Main()
{
PhoneBook pl = new PhoneBook();
Console.WriteLine("Please Enter Your Name:");
pl.name=Console.ReadLine();
Console.WriteLine("Please Enter Your Age:");
string s = Console.ReadLine();
pl.age= Convert.ToByte(s);
Console.WriteLine("Please Enter Your Phone:");
s = Console.ReadLine();
pl.phone=Convert.ToByte(s);
Console.WriteLine("{0}{1}{2}",pl.name,pl.age,pl.phone);
}
}
using System;
struct PhoneBook
{
public string name;
public byte age;
public byte phone;
}class test
{
static void Main()
{
PhoneBook pl = new PhoneBook();
Console.WriteLine("Please Enter Your Name:");
pl.name=Console.ReadLine();
Console.WriteLine("Please Enter Your Age:");
string s = Console.ReadLine();
pl.age= Convert.ToByte(s);
Console.WriteLine("Please Enter Your Phone:");
s = Console.ReadLine();
pl.phone=Convert.ToByte(s);
Console.WriteLine("{0}{1}{2}",pl.name,pl.age,pl.phone);
}
}
int b = 0;
//int b = 2;
try
{
int c = a / b;
}
catch
{
Console.WriteLine("error");
}如果b=0,是不合法的,会出错,结束程序;用了try-catch就可以自己来处理错误。
不过这样来判断输入的是不是数,做成while循环,好想不太方便。
bool result = false;
do
{
try
{
Console.WriteLine("Please Enter Your Age:");
pl.age=byte.Parse( Console.ReadLine() );
result = true; //上面那句出了错,就不会执行这句,便会继续循环,再输入
}
catch
{ //出错处理
Console.WriteLine("Error,Please re-input");
pl.age = 0; //不打这句,会编译不过
}
}while(!result);
这个方法好象不太好。一个很简单的输入,写了一大段。
>>Console.WriteLine("Please Enter Your Age:");
>>pl.age=byte.Parse( Console.ReadLine() ); //把string转成byte类型,但得保证用户输入的是0-255的数值;要防止出错得用try-catch来处理换成
bool
do{
.
.
}while