2016-11-24 15 views
2

私はC#を初めてお使いで、次の問題があります。if/else else/else

車両に支払う義務を計算するプログラム。ユーザーは車の種類(3種類あります)、ライセンスの長さ(6または12ヶ月)、排出帯(5種類)を入力し、ライセンスのコストを印刷します。

私は6ヶ月のライセンスの長さを選ぶ限り長く。

私が間違いを犯したことを理解するのを助けてください、ありがとうございます。 length == 12ため

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

ご提供されたコードに問題:

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

が改善::

ユーザーが12

てみてください選択したときにあなたはbandためのユーザ入力を取得されていません

重要コードを書くことは(特にあなたが提供したコードの中で)読みやすさを心配し、コードを重複させる必要がないかどうかを確認することです。

すでに多くのコメントが暗示しているように、コードをどのように記述したかの構造は最適ではありません。 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

枝はbandの値を求めるプロンプトが表示されません。それらのブランチでもその質問を複製するか、コードの一部を変更して複製の一部を削除する必要があります。

3

あなたはあなたのコード内の概念のような多数の問題を抱えているが、ので、なぜ機能していない、あなたの問題がある:あなたがif(length == 6)

どのように外のバンドについて質問する必要があるので、

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 
} 

コードを改善することができます。ネストされているのは本当に悪い習慣なので、矢印の構造を持たずにコードを書くことを常に考えなければなりません。

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ヶ月のライセンス期間の条件でEmission Bandを入力しませんでした。`bool check; 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

if else else else switchキーワードを使用してください。