2016-10-07 6 views
0

私が達成しようとしていることをかなり明確にして、以前の選択に基づいて異なるオプションツリーを作成する必要があります。何らかの理由でその私がコメントを残して両方の場所で期待されるエラーを「}投げ。私が何か間違ったことをやっているか?これも私が?コンソールアプリケーションでオプションツリーを作成する

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double TotalAmount; 
      TotalAmount = 300.7 + 75.60; 
      Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount); 
      Console.WriteLine("Would you like to withdraw money? Please type yes or no."); 
      string UserInput2 = Console.ReadLine(); 
      if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?"); 
      { 
       string UserInput = Console.ReadLine(); 
       double UserInput3 = double.Parse(UserInput); 
       TotalAmount = TotalAmount - UserInput3; 
       Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount); 
       Console.WriteLine("Anything else?"); 
       string UserInput4 = Console.ReadLine(); 

       if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?"); 
       Console.WriteLine("1). Withdraw"); 
       Console.WriteLine("2). Deposit"); 
       Console.WriteLine("Use number key for selection"); 
       string UserInput5 = Console.ReadLine(); 
       if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?"); 
       else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?"); 
       string UserInput6 = Console.ReadLine(); //FIRST ERROR 

      else if (UserInput4.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
       { 
        System.Environment.Exit(1); 
       } 
      } //SECOND ERROR 
      else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
      { 
       System.Environment.Exit(1); 
      } 
     } 
    } 
} 

答えて

0

あなたの問題は、あなたのコードの構造をすることが非常に困難であるということであるたいように動作します読みしたがって、あなたは問題が基本的に、あなたが適切にあなたの中括弧を閉じていなかったことについての推論を持ってこれを見てください:。。

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double TotalAmount; 
      TotalAmount = 300.7 + 75.60; 
      Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount); 
      Console.WriteLine("Would you like to withdraw money? Please type yes or no."); 
      string UserInput2 = Console.ReadLine(); 
      string UserInput4; 
      if (UserInput2.ToLower() == "yes") 
      { 
       Console.WriteLine("How much would you like to withdraw?"); 
       string UserInput = Console.ReadLine(); 
       double UserInput3 = double.Parse(UserInput); 
       TotalAmount = TotalAmount - UserInput3; 
       Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount); 
       Console.WriteLine("Anything else?"); 
       UserInput4 = Console.ReadLine(); 
       if (UserInput4.ToLower() == "yes") 
       { 
        Console.WriteLine("What do you want to do?"); 
       } 
       Console.WriteLine("1). Withdraw"); 
       Console.WriteLine("2). Deposit"); 
       Console.WriteLine("Use number key for selection"); 
       string UserInput5 = Console.ReadLine(); 
       if (UserInput5.ToLower() == "1") 
       { 
        Console.WriteLine("How much would you like to withdraw?"); 
       } 
       else if (UserInput5.ToLower() == "2") 
       { 
        Console.WriteLine("How much would you like to deposit?"); 
       } 
       string UserInput6 = Console.ReadLine(); 
      } 
      else if (UserInput2.ToLower() == "no") 
      { 
       Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
       System.Environment.Exit(1); 
      } 
      else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
      { 
       System.Environment.Exit(1); 
      } 
     } 
    } 
} 

私はあなたが声明「あればそうでない」「場合」またはを持っている時はいつでもすることを示唆しています中括弧で囲まれたコードブロックを常に1つのライナーにしておくと、多くの苦痛を軽減します。 また、sorr私の答えであなたのアプリケーションのロジックを台無しにしたらy。しかし、あなたの変数UserInput1、UserInput2などの名前をつけます。 "wouldLikeToWithdrawAnswer"、 "howMuchAnswer"などの意味のある名前を付けた方がずっと簡単です。これらの変数すべてでちょっと失われてしまいました:)

+0

ああ、このすべてで、まだ本当に新しい申し訳ありませんが、理にかなっています。あなたのコードをコピー/貼り付け、それはエラーを出すことなく動作しましたが、私はそれを自分でコピーして、それをすべて試してみましたが、うまくいきませんでした。これが私を馬鹿のように聞こえるのですが、インデントされた括弧や行がどのように重要になるのでしょうか? – Anath3ma

+0

https://dotnetfiddle.net/uKyiO7これは今の私の見た目ですが、依然としてブレースエラーがあります – Anath3ma

+0

ああ、今は気にしません。私はまだ ";"カップルラインの終わりに。あなたの助けをありがとう、本当にそれを感謝:) – Anath3ma

0

コードを書く方法は、誰かが理解することが非常に困難です。 あなたは多くを改善する必要があります。ここで働くExample on .NET Fiddle

public static void Main() 
    { 
      double TotalAmount; 
      TotalAmount = 300.7 + 75.60; 
      Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount); 
      Console.WriteLine("Would you like to withdraw money? Please type yes or no."); 
      string UserInput2 = Console.ReadLine(); 
      if (UserInput2.ToLower() == "yes") 
      { 
       Console.WriteLine("How much would you like to withdraw?"); 
       string UserInput = Console.ReadLine(); 
       double UserInput3 = double.Parse(UserInput); 
       TotalAmount = TotalAmount - UserInput3; 
       Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount); 
       Console.WriteLine("Anything else?"); 
       string UserInput4 = Console.ReadLine(); 

       if (UserInput4.ToLower() == "yes") 
       { 
        Console.WriteLine("What do you want to do?"); 
        Console.WriteLine("1). Withdraw"); 
        Console.WriteLine("2). Deposit"); 
        Console.WriteLine("Use number key for selection"); 
        string UserInput5 = Console.ReadLine(); 
        if (UserInput5.ToLower() == "1") 
        { 
         Console.WriteLine("How much would you like to withdraw?"); 
        } 
        else if (UserInput5.ToLower() == "2") 
        { 
         Console.WriteLine("How much would you like to deposit?"); 
         string UserInput6 = Console.ReadLine(); //FIRST ERROR 
        } 
       } 
      else if (UserInput4.ToLower() == "no") 
       { 
        Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
        System.Environment.Exit(1); 
       } 
      } //SECOND ERROR 
      else if(UserInput2.ToLower() == "no") 
      { 
       Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'("); 
       System.Environment.Exit(1); 
      } 
     } 
0

ある問題は、あなたがそれだと思うどこifブロックは行っていないということです。

if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?"); 
// And that's the if statement done. 
     { 
      // This is not part of any if block 
      string UserInput = Console.ReadLine(); 
      double UserInput3 = double.Parse(UserInput); 
      TotalAmount = TotalAmount - UserInput3; 
      Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount); 
      Console.WriteLine("Anything else?"); 
      string UserInput4 = Console.ReadLine(); 

      if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?"); 
      Console.WriteLine("1). Withdraw"); 
      Console.WriteLine("2). Deposit"); 
      Console.WriteLine("Use number key for selection"); 
      string UserInput5 = Console.ReadLine(); 
      if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?"); 
      else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?"); 
      string UserInput6 = Console.ReadLine(); //FIRST ERROR 

     else //Compiler says "else? There is no if to match this with!" 
関連する問題