2017-08-06 1 views
0

Ich bin sehr Anfänger, wenn es um die Programmierung von C# geht, gibt es zwei Variablen in einer Formel, die nicht als ganze Zahlen analysieren, obwohl ich sie innerhalb der Hauptfunktion deklariert. Ich habe mehrere Methoden, kleine Aufgaben zu erreichen, und es scheint nicht die Hauptvariable mit der Methode zu verknüpfen, der Rest des Codes hat keine Fehler, so dass ich nicht sicher bin, warum es die Formel nicht macht. (Ich werde den Code zu schreiben haben alle um u zu zeigen)Visual C# - Variable existiert nicht im aktuellen Kontext

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

Da int Werttyp ist, wird der Wert übergeben, sodass die Änderungen in der Hauptmethode nicht sichtbar sind. Siehe https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value für detaillierte Informationen über das Konzept. – Smit

+0

In C# müssen Sie 'AskForIncome (ref int income)' als Referenz verwenden. – Smit

Antwort

0

C# seiner oop Sprache, in Ihrem Beispiel sieht aus wie Sie in C entwickelt, Zunächst müssen Sie eine Klasse tun, das wird haben alle Variablen und Funktionen \ Methoden, die Sie brauchen semething wie:

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."); 
      } 
     } 
} 

und von Haupt beschäftigen Objekt erstellen und tun alles, was Sie brauchen, sondern lernen oop Programmierung es muss ist.

Verwandte Themen