gpt4 book ai didi

c# - 如果/如果其他/其他

转载 作者:行者123 更新时间:2023-11-30 23:18:35 26 4
gpt4 key购买 nike

我是 C# 新手,遇到以下问题。

计算车辆应缴纳税款的程序。用户输入汽车类型(有 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);
}

最佳答案

你的代码中有多个概念问题,但你的问题为什么不起作用是因为:

if (length == 6)
{
Console.WriteLine("Enter Emission Band (AA, A, B, C, D): ");
band = Console.ReadLine();
//..other stuff
}
else if (length == 12)
{
//here band has his initial value
}

所以你需要询问if(length == 6)

之外的band

如何改进代码。嵌套 if 是非常糟糕的做法,因此您应该始终考虑编写没有箭头结构的代码(嵌套 if)。

public static void Main()
{
int car = 0;
int length = 0;
Console.WriteLine("Enter Car Type (TC #): ");

if(!int.TryParse(Console.ReadLine(), out car))
{
Console.WriteLine("Not valid number");
return;
}

Console.WriteLine("Enter Licience length in months(6 or 12)");
if(!int.TryParse(Console.ReadLine(), out length))
{
Console.WriteLine("Not valid number");
return;
}

Console.WriteLine("Enter Emission Band (AA, A, B, C, D): ");
string band = Console.ReadLine();

Dictionary<Tuple<int,int,string>, decimal> carDataDic = GetCarDetails();

decimal ratio = 0;

Tuple<int, int, string> checkRatioKey = new Tuple<int, int, string>(car, length, band);

if(!carDataDic.TryGetValue(checkRatioKey, out ratio))
{
Console.WriteLine("No value found for input data");
return;
}

Console.WriteLine("Ratio is: " + ratio);
}

public static Dictionary<Tuple<int,int,string>, decimal> GetCarDetails()
{
Dictionary<Tuple<int,int,string>, decimal> values= new Dictionary<Tuple<int,int,string>, decimal>();

//Tuple Items -> Item1=car, Item2=length, Item3= band), value in Dictionary is the rate which you should have
values.Add(new Tuple<int, int, string>(49, 6, "AA"), 44);
values.Add(new Tuple<int, int, string>(49, 6, "A"), 60.5);
//define all of your cases.

return values;
}

Tuple 具有类的作用,您可以在其中存储特定于汽车的数据。它很好用,因为您可以将它用作 Dictionary 中的键。

关于c# - 如果/如果其他/其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com