2017-08-06 15 views
0

私は非常に初心者です。プログラミングでは、#には2つの変数がありますが、それらはmain関数内で宣言していますが、整数として解析しません。私は小さなタスクを達成するために複数のメソッドを持っていますが、メイン変数をメソッド1にリンクしているようには見えません。コードの残りの部分にエラーがないので、なぜフォーミュラをやっていないのか分かりません。あなたがCで開発されたように、あなたがその意志クラスを行うために必要なすべての まず探しますあなたの例では#そのOOP言語、CVisual C# - 変数が現在のコンテキストに存在しません

public static void Main() 
    { 
     int income = 0; 
     int children = 0; 
     int tax = 0; 
     AskForIncome(income); 
     NoChild(children); 
     CalculateTax(tax); 
     ExitProgram(); 
    } 

    public static void AskForIncome(int income) 
    { 
     Console.WriteLine("What is your total income: "); 
     string testNum = Console.ReadLine(); 
     bool dummyBool = int.TryParse(testNum, out income); 
     if (dummyBool) 
     { 
      Console.WriteLine(); 
     } 
     else if(income < 0) 
     { 
      Console.WriteLine("Your income cannot be negative."); 
     } 
     else 
     { 
      Console.WriteLine("Enter your income as a whole-dollar numeric figure."); 
     } 
    } 
    public static void NoChild(int children) 
    { 

     Console.WriteLine("How many children do you have: "); 
     string testChild = Console.ReadLine(); 
     bool dummyBool2 = int.TryParse(testChild, out children); 
     if (dummyBool2) 
     { 
      Console.WriteLine(); 
     } 
     else if(children < 0) 
     { 
      Console.WriteLine("You must enter a positive number."); 
     } 
     else 
     { 
      Console.WriteLine("You must enter a valid number."); 
     } 

    } 
    public static void CalculateTax(int tax) 
    { 
     tax = income - 10000 - (children * 2000); 
     if (tax < 0) 
     { 
      Console.WriteLine("You owe no tax."); 
     } 
     else 
     { 
      Console.WriteLine("You owe a total of $" + tax + " tax."); 
     } 
    } 
+0

intは値型であるため、変更はメインメソッドでは表示されないように値渡しを行っています。概念の詳細については、https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-valueを参照してください。 – Smit

+0

C#では、 'AskForIncome(ref int income)'を使用して参照渡しする必要があります。 – Smit

答えて

0

を(私はUを表示するために、すべてのコードをポストする必要があります)すべての変数と関数\方法あなたのようsemethingする必要があります。

public class Employ 
{ 
    public int income {get; set;} 
    public int children {get; set} 
    public int tax {get; set}; 

      public static void AskForIncome() 
     { 
      Console.WriteLine("What is your total income: "); 
      string testNum = Console.ReadLine(); 
      bool dummyBool = int.TryParse(testNum, out income); 
      if (dummyBool) 
      { 
       Console.WriteLine(); 
      } 
      else if (income < 0) 
      { 
       Console.WriteLine("Your income cannot be negative."); 
      } 
      else 
      { 
       Console.WriteLine("Enter your income as a whole-dollar numeric figure."); 
      } 
     } 
     public static void NoChild() 
     { 

      Console.WriteLine("How many children do you have: "); 
      string testChild = Console.ReadLine(); 
      bool dummyBool2 = int.TryParse(testChild, out children); 
      if (dummyBool2) 
      { 
       Console.WriteLine(); 
      } 
      else if (children < 0) 
      { 
       Console.WriteLine("You must enter a positive number."); 
      } 
      else 
      { 
       Console.WriteLine("You must enter a valid number."); 
      } 

     } 
     public static void CalculateTax() 
     { 
      tax = income - 10000 - (children * 2000); 
      if (tax < 0) 
      { 
       Console.WriteLine("You owe no tax."); 
      } 
      else 
      { 
       Console.WriteLine("You owe a total of $" + tax + " tax."); 
      } 
     } 
} 

とメイン採用オブジェクトを作成し、必要なものすべてを行うが、それは必須だオブジェクト指向プログラミングを学ぶから

関連する問題