2016-11-24 186 views
2

我是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); 
    } 
+2

你也可以使用'switch'语句。 –

+1

我建议做一个基本的编程课程。因为你的代码显示缺乏对如何编写...程序的理解。只是一个例子:为了减少if/else分支的数量,你应该使用'Dictionary' – MrPaulch

回答

0

的问题,您提供的代码:

你没有得到用户输入band当用户选择12

尝试:

else if (length == 12) 
{ 
    Console.WriteLine("Enter Emission Band (AA, A, B, C, D): "); 
    band = Console.ReadLine(); 
    //etc ... 

改进:

重要时编写代码(特别是在您提供的代码中)是担心可读性,并查看我们是否需要重复代码。

正如众多评论暗示的那样,您编写代码的结构远非最佳。已经提到了Tuples肯定是一个好方法。

我的例子演示了如何使用Car class来代替,并且是另一种可能的改进方案。

class Program 
{ 
    public static List<Car> CarList; 

    static void Main(string[] args) 
    { 

     PopulateCars(); 

     int car, length; 
     decimal rate = 0m; 
     Car.CarBand 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#): "); 
     var keyboardInput = Console.ReadLine(); 

     while (!int.TryParse(keyboardInput, out car)) 
     { 
      Console.WriteLine("Invalid car input, try again."); 
      keyboardInput = Console.ReadLine(); 
     } 

     Console.WriteLine("Enter Licience length in months(6 or 12)"); 
     keyboardInput = Console.ReadLine(); 

     while (!int.TryParse(keyboardInput, out length)) 
     { 
      Console.WriteLine("Invalid months input, try again."); 
      keyboardInput = Console.ReadLine(); 
     } 

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

     while (!Enum.TryParse(keyboardInput, out band)) 
     { 
      Console.WriteLine("Invalid band input, try again."); 
      keyboardInput = Console.ReadLine(); 
     } 

     var matchedCar = CarList.FirstOrDefault(c => c.CarNumber == car && c.Lenght == length && c.Band == band); 

     if (matchedCar != null) 
      Console.WriteLine("The rate for this car is {0}", matchedCar.Rate); 
     else 
      Console.WriteLine("Car not found"); 

     Console.ReadLine(); 

    } 

    public class Car 
    { 
     public int CarNumber { get; set; } 
     public int Lenght { get; set; } 
     public CarBand Band { get; set; } 
     public decimal Rate { get; set; } 

     public enum CarBand 
     { 
      AA, 
      A, 
      B, 
      C, 
      D 
     } 
    } 

    public static void PopulateCars() 
    { 
     CarList = new List<Car>(); 

     // Cars 48 
     CarList.Add(new Car { CarNumber = 48, Lenght = 6, Band = Car.CarBand.AA, Rate = 38.5m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 6, Band = Car.CarBand.A, Rate = 55m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 6, Band = Car.CarBand.B, Rate = 66m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 6, Band = Car.CarBand.C, Rate = 77m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 6, Band = Car.CarBand.D, Rate = 85.25m }); 

     CarList.Add(new Car { CarNumber = 48, Lenght = 12, Band = Car.CarBand.AA, Rate = 70m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 12, Band = Car.CarBand.A, Rate = 100m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 12, Band = Car.CarBand.B, Rate = 120m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 12, Band = Car.CarBand.C, Rate = 140m }); 
     CarList.Add(new Car { CarNumber = 48, Lenght = 12, Band = Car.CarBand.D, Rate = 155m }); 

     //Cars 49 
     CarList.Add(new Car { CarNumber = 49, Lenght = 6, Band = Car.CarBand.AA, Rate = 44m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 6, Band = Car.CarBand.A, Rate = 60.5m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 6, Band = Car.CarBand.B, Rate = 71.5m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 6, Band = Car.CarBand.C, Rate = 82.5m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 6, Band = Car.CarBand.D, Rate = 88m }); 

     CarList.Add(new Car { CarNumber = 49, Lenght = 12, Band = Car.CarBand.AA, Rate = 80m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 12, Band = Car.CarBand.A, Rate = 110m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 12, Band = Car.CarBand.B, Rate = 130m}); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 12, Band = Car.CarBand.C, Rate = 150m }); 
     CarList.Add(new Car { CarNumber = 49, Lenght = 12, Band = Car.CarBand.D, Rate = 160m });   

     // Cars 59 
     CarList.Add(new Car { CarNumber = 59, Lenght = 6, Band = Car.CarBand.AA, Rate = 33m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 6, Band = Car.CarBand.A, Rate = 49.5m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 6, Band = Car.CarBand.B, Rate = 60.5m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 6, Band = Car.CarBand.C, Rate = 71.5m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 6, Band = Car.CarBand.D, Rate = 82.5m }); 

     CarList.Add(new Car { CarNumber = 59, Lenght = 12, Band = Car.CarBand.AA, Rate = 60m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 12, Band = Car.CarBand.A, Rate = 90m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 12, Band = Car.CarBand.B, Rate = 110m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 12, Band = Car.CarBand.C, Rate = 130m }); 
     CarList.Add(new Car { CarNumber = 59, Lenght = 12, Band = Car.CarBand.D, Rate = 150m });   
    } 

} 
+1

这可能解决眼前的问题......但是OP可能对...编程有一个基本的误解。这很可能会有进一步的复杂性。 – MrPaulch

+0

@MrPaulch我只是回答了具体的直接问题*“只要我选择6个月许可证长度,我的代码就能完美工作”*。没有必要详细说明某人如何编程。 – Jim

+0

我不质疑这个事实,你的答案解决了OP的问题。如果OP使用*错误的*工具解决问题,那么可以谨慎地提及这一点。作为奖励可以建议更好的工具 – MrPaulch

0

length == 12分支机构不提示为band的值。您还需要在这些分支上复制该问题,否则请更改您的代码以删除一些重复项。

3

您有多个问题,在你的代码的概念,但是你的问题,为什么不工作是因为:

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)

如何外你可以改进你的代码。嵌套如果是非常糟糕的做法,所以你应该总是认为编写代码时没有箭头结构(嵌套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的密钥中使用它。

0

您在12个月的许可证长度条件下没有收到发射带的输入。`布尔检查; int car,length; 双倍率= 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) 
       { 
        Console.WriteLine("Enter Emission Band (AA, A, B, C, D): "); 
        band = Console.ReadLine(); 
        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) 
       { 
        Console.WriteLine("Enter Emission Band (AA, A, B, C, D): "); 
        band = Console.ReadLine(); 
        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) 
       { 
        Console.WriteLine("Enter Emission Band (AA, A, B, C, D): "); 
        band = Console.ReadLine(); 
        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); 
     Console.ReadLine();` 
0

更好的代码看起来是这样的:

// Dictionary from (car,length,band) to rate 
static readonly Dictionary<Tuple<int, int, string>, decimal> rates = 
    new Dictionary<Tuple<int, int, string>, decimal> 
    { 
    { Tuple.Create(49, 6, "AA"), 44m }, 
    { Tuple.Create(49, 6, "A"), 60.5m }, 
    { Tuple.Create(49, 6, "B"), 71.5m }, 
    { Tuple.Create(49, 6, "C"), 82.5m }, 
    { Tuple.Create(49, 6, "D"), 88m }, 
    { Tuple.Create(49, 12, "AA"), 80m }, 
    { Tuple.Create(49, 12, "A"), 110m }, 
    { Tuple.Create(49, 12, "B"), 130m }, 
    { Tuple.Create(49, 12, "C"), 150m }, 
    { Tuple.Create(49, 12, "D"), 160m }, 
    { Tuple.Create(48, 6, "AA"), 38.5m }, 
    { Tuple.Create(48, 6, "A"), 55m }, 
    { Tuple.Create(48, 6, "B"), 66m }, 
    { Tuple.Create(48, 6, "C"), 77m }, 
    { Tuple.Create(48, 6, "D"), 85.25m }, 
    { Tuple.Create(48, 12, "AA"), 70m }, 
    { Tuple.Create(48, 12, "A"), 100m }, 
    { Tuple.Create(48, 12, "B"), 120m }, 
    { Tuple.Create(48, 12, "C"), 140m }, 
    { Tuple.Create(48, 12, "D"), 155m }, 
    { Tuple.Create(59, 6, "AA"), 33m }, 
    { Tuple.Create(59, 6, "A"), 49.5m }, 
    { Tuple.Create(59, 6, "B"), 60.5m }, 
    { Tuple.Create(59, 6, "C"), 71.5m }, 
    { Tuple.Create(59, 6, "D"), 82.5m }, 
    { Tuple.Create(59, 12, "AA"), 60m }, 
    { Tuple.Create(59, 12, "A"), 90m }, 
    { Tuple.Create(59, 12, "B"), 110m }, 
    { Tuple.Create(59, 12, "C"), 130m }, 
    { Tuple.Create(59, 12, "D"), 150m }, 
    }; 

static void Main() 
{ 
    Console.WriteLine("Diesel Car Petrol Car Alt. Fuel Car"); 
    Console.WriteLine("TC 49  TC 48  TC 59"); 
    Console.WriteLine("Enter Car Type (TC#): "); 
    int car; 
    if (!int.TryParse(Console.ReadLine(), out car)) 
    { 
    Console.WriteLine("Error"); 
    return; 
    } 

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

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

    var key = Tuple.Create(car, length, band); 
    decimal rate; 
    if (!rates.TryGetValue(key, out rate) 
    { 
    Console.WriteLine("Error finding rate for " + key); 
    return; 
    } 
    Console.WriteLine(rate); 
} 

这基本上是他的回答提出的相同mybirthname。

备注:在即将到来的C#7.0中(预计在2017年),我认为将会有更好的元组语法。

-1

使用开关关键字进入这个问题,而不是如果,否则,否则