C# Console App Age In One Year

C# Console App Age In One Year

27/04/2020 0 By Admin

This C# console app asks the user to enter their age.

It  prints to the console screen the use’s in one years time.

Notes

To understand and build this console app you will need:

  • Data types: string, integer.
  • variables.
  • Write and WriteLine methods.
  • ReadLine method.
  • int.Parse method.
  • ReadKey method.
  • Parameter place holder {0}.

Explanation

Line 15 declares a variable of integer data type (which is age).

Ling 16 declares a variable of the string data type (which is aValue).

Line 17 uses the Write method to ask user for their age.

Line 18 initialises the aValue variable by assigning it to the input requested by the ReadLine Method.

Line 19 initialises the age variable by assigning it to aValue.  Remember that aValue contains the age entered at ReadLine…

Line 19 (continued)  … but because aValue exists as a string, we need to convert it into an integer.

Line 19 (continued) … the variable aValue is now of the integer data type.

Line 20 The WriteLine method prints to the screen the text in between ”   “, and the age entered plus 1.

Line 21 This is the end of the user’s input. Any keypress will close the app.

Variations

  • Try adding a different value such as 12 (replace 1).
  • Try changing the text in the WriteLine method.
  • Try adding two WriteLine methods. One that calculates age in 12 years and one that calculates age in 22 years.