Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MyFirstFunction
- {
- class MyFirstFunctionTrynumber4
- {
- static void Main(string[] args)
- {
- // We get the first Integer
- Console.Write("\nPlease enter an integer value: "); //Asks the user to basically enter a number
- string enteredNumberText = Console.ReadLine();
- int enteredNumber = 0; //Initialize a variable to store the first number.
- if (!int.TryParse(enteredNumberText, out enteredNumber)) //This will convert the number the string to a number or if this is not possible the error message will display
- // In English this would be: If the result that is returned from the TryParse function is not an integer then print this error message.
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("The input entered ,{0},does not appear to be a number", enteredNumberText);
- Console.ResetColor();
- Console.WriteLine("Hit <Enter> to Exit");
- Console.Read();// waits for the user to hit a key...
- return;
- }
- // We get the second Integer
- //Note -> the fact that I cut and pasted this then changed the integer names really strikes me a bad coding, my suspicion is that this would be easier to do with a function - something to work on later
- Console.Write("\nPlease enter a second integer value: "); //Asks the user to basically enter a number
- string enteredNumberText2 = Console.ReadLine();
- int enteredNumber2 = 0; //Initialize a variable to store the first number.
- if (!int.TryParse(enteredNumberText2, out enteredNumber2))
- {
- Console.WriteLine("The input entered ,{0},does not appear to be a number", enteredNumberText2);
- Console.ResetColor();
- Console.WriteLine("Hit <Enter> to Exit");
- Console.Read();// waits for the user to hit a key...
- return;
- }
- // Calls the function to add up the two integers at this stage.
- long integerSum = SumOfIntegers(enteredNumber, enteredNumber2);
- //Outputs data to the user summarizing the entered values and their sum
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine();
- Console.WriteLine("The first integer is ,{0}, and the second integer is ,{1}, and their sum is: {2}", enteredNumber, enteredNumber2, integerSum);
- Console.WriteLine();
- Console.WriteLine("{0} + {1} = {2}", enteredNumber, enteredNumber2, integerSum);
- //Pause to end the program
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine();
- Console.WriteLine("\nType <Enter> to Exit.");
- Console.ResetColor();//Reset the console color to the default.
- Console.ReadKey();
- }
- static long SumOfIntegers(int First, int Second)
- {
- int Total = First + Second;
- return Total;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement