2016-11-24 138 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

使用開關關鍵字進入這個問題,而不是如果,否則,否則