曾驰文 发表于 2012-4-5 15:48
public class Ticket
{
///
这同学写的是对的哦,不过有些地方还可以优化,我优化了一下.
//program里的
static void Main(string[] args)
{
//行驶的距离
int result;
Console.WriteLine("请您输入行驶的距离");
string distance = Console.ReadLine();
bool judge = false;
while (judge==false)
{
if (int.TryParse( distance,out result))
{
Ticket tick = new Ticket(Convert.ToInt32(distance));
judge = true;
}
else
{
Console.WriteLine("输入的格式错误,请重新输入");
distance=Console.ReadLine();
}
}
Console.ReadKey();
}
//ticket里面的
public class Ticket
{
/// <summary>
/// 行驶的距离
/// </summary>
private int distance;
public int Distance
{
get
{
return distance;
}
}
/// 构造函数
/// 构造函数
/// </summary>
/// <param name="dis">相应的距离</param>
public Ticket(int distance)
{
this.distance = distance;
double total = 0;
if (distance <= 100)// 100公里之内不打折
{
total = distance;
}
else if (distance > 101 && distance <= 200)//101-200公里之内打9.5折
{
total = distance * 0.95;
}
else if (distance > 201 && distance <= 300)//201-3000公里之内打9.0折
{
total = distance * 0.9;
}
else if (distance > 300)//300公里以上打8折
{
total = distance * 0.8;
}
//打印相应的票价
Console.WriteLine("您的票价是:{0}", total);
}
}
这样应该就是答案了 |