我是C#的新手,遇到以下问题。if/if else/else
一个程序来计算在车辆上支付的责任。用户输入汽车类型(有3种类型),许可证的长度(6或12个月)和发射带(5种),并且程序打印许可证的成本:
我的代码完美地工作只要我选择6个月的许可证长度。
请帮我理解我做了什么错误,谢谢。
static void Main(string[] args)
{
bool check;
int car, length;
double rate = 0;
string band = "";
Console.WriteLine("{0,-12} {1,-12} {2,-12}", "Diesel Car", "Petrol Car", "Alt. Fuel Car");
Console.WriteLine("{0,-12} {1,-12} {2,-12}", "TC 49", "TC 48", "TC 59");
Console.WriteLine("Enter Car Type (TC#): ");
check = int.TryParse(Console.ReadLine(), out car);
if (check)
{
if (car == 49)
{
Console.WriteLine("Enter Licience length in months(6 or 12)");
length = int.Parse(Console.ReadLine());
if (length == 6)
{
Console.WriteLine("Enter Emission Band (AA, A, B, C, D): ");
band = Console.ReadLine();
if (band == "AA")
{
rate = 44;
}
else if (band == "A")
{
rate = 60.5;
}
else if (band == "B")
{
rate = 71.5;
}
else if (band == "C")
{
rate = 82.5;
}
else if (band == "D")
{
rate = 88;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else if (length == 12)
{
if (band == "AA")
{
rate = 80;
}
else if (band == "A")
{
rate = 110;
}
else if (band == "B")
{
rate = 130;
}
else if (band == "C")
{
rate = 150;
}
else if (band == "D")
{
rate = 160;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else
Console.WriteLine("ERROR"); // error for length != 6 or 12
}
else if (car == 48)
{
Console.WriteLine("Enter Licience length in months(6 or 12)");
length = int.Parse(Console.ReadLine());
if (length == 6)
{
Console.WriteLine("Enter Emission Band (AA, A, B, C, D): ");
band = Console.ReadLine();
if (band == "AA")
{
rate = 38.5;
}
else if (band == "A")
{
rate = 55;
}
else if (band == "B")
{
rate = 66;
}
else if (band == "C")
{
rate = 77;
}
else if (band == "D")
{
rate = 85.25;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else if (length == 12)
{
if (band == "AA")
{
rate = 70;
}
else if (band == "A")
{
rate = 100;
}
else if (band == "B")
{
rate = 120;
}
else if (band == "C")
{
rate = 140;
}
else if (band == "D")
{
rate = 155;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else
Console.WriteLine("ERROR"); // error for length != 6 or 12
}
else if (car == 59)
{
Console.WriteLine("Enter Licience length in months(6 or 12)");
length = int.Parse(Console.ReadLine());
if (length == 6)
{
Console.WriteLine("Enter Emission Band (AA, A, B, C, D): ");
band = Console.ReadLine();
if (band == "AA")
{
rate = 33;
}
else if (band == "A")
{
rate = 49.5;
}
else if (band == "B")
{
rate = 60.5;
}
else if (band == "C")
{
rate = 71.5;
}
else if (band == "D")
{
rate = 82.5;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else if (length == 12)
{
if (band == "AA")
{
rate = 60;
}
else if (band == "A")
{
rate = 90;
}
else if (band == "B")
{
rate = 110;
}
else if (band == "C")
{
rate = 130;
}
else if (band == "D")
{
rate = 150;
}
else
Console.WriteLine("ERROR"); //error for band != AA,A,B,C or D
}
else
Console.WriteLine("ERROR"); // error for length != 6 or 12
}
else
Console.WriteLine("ERROR"); // error for car number != 48,49 or 59
}
else
Console.WriteLine("ERROR"); //error for car num != int
Console.WriteLine(rate);
}
你也可以使用'switch'语句。 –
我建议做一个基本的编程课程。因为你的代码显示缺乏对如何编写...程序的理解。只是一个例子:为了减少if/else分支的数量,你应该使用'Dictionary' – MrPaulch