Advertisement
Guest User

My First Function ! - now working!!!

a guest
Mar 5th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MyFirstFunction
  4. {
  5. class MyFirstFunctionTrynumber4
  6. {
  7. static void Main(string[] args)
  8. {
  9. // We get the first Integer
  10. Console.Write("\nPlease enter an integer value: "); //Asks the user to basically enter a number
  11. string enteredNumberText = Console.ReadLine();
  12. int enteredNumber = 0; //Initialize a variable to store the first number.
  13.  
  14. 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
  15. // In English this would be: If the result that is returned from the TryParse function is not an integer then print this error message.
  16. {
  17. Console.ForegroundColor = ConsoleColor.Red;
  18. Console.WriteLine("The input entered ,{0},does not appear to be a number", enteredNumberText);
  19. Console.ResetColor();
  20. Console.WriteLine("Hit <Enter> to Exit");
  21. Console.Read();// waits for the user to hit a key...
  22. return;
  23. }
  24.  
  25. // We get the second Integer
  26. //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
  27. Console.Write("\nPlease enter a second integer value: "); //Asks the user to basically enter a number
  28. string enteredNumberText2 = Console.ReadLine();
  29. int enteredNumber2 = 0; //Initialize a variable to store the first number.
  30.  
  31. if (!int.TryParse(enteredNumberText2, out enteredNumber2))
  32. {
  33. Console.WriteLine("The input entered ,{0},does not appear to be a number", enteredNumberText2);
  34. Console.ResetColor();
  35. Console.WriteLine("Hit <Enter> to Exit");
  36. Console.Read();// waits for the user to hit a key...
  37. return;
  38. }
  39.  
  40. // Calls the function to add up the two integers at this stage.
  41.  
  42. long integerSum = SumOfIntegers(enteredNumber, enteredNumber2);
  43.  
  44. //Outputs data to the user summarizing the entered values and their sum
  45. Console.ForegroundColor = ConsoleColor.Cyan;
  46. Console.WriteLine();
  47. Console.WriteLine("The first integer is ,{0}, and the second integer is ,{1}, and their sum is: {2}", enteredNumber, enteredNumber2, integerSum);
  48. Console.WriteLine();
  49. Console.WriteLine("{0} + {1} = {2}", enteredNumber, enteredNumber2, integerSum);
  50.  
  51. //Pause to end the program
  52. Console.ForegroundColor = ConsoleColor.Magenta;
  53. Console.WriteLine();
  54. Console.WriteLine("\nType <Enter> to Exit.");
  55. Console.ResetColor();//Reset the console color to the default.
  56. Console.ReadKey();
  57. }
  58. static long SumOfIntegers(int First, int Second)
  59. {
  60. int Total = First + Second;
  61. return Total;
  62. }
  63.  
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement