(Concepts: int, double, operations and conversions)
Do you know the difference between an int, double and string?
We are going to make a working calculator. It’s a minimalist calculator, but it can calculate things for you that you can’t do yourself. How much is 655 times 23623? Most people prefer to use a program for that. You can make that program.
We ask the user to type in a number (and press enter), another number (enter again), then we tell the user how much you get when you add the numbers, as well as how much you get when you multiply the numbers.
Console.WriteLine("cauliflower")
you can show the literal text
between the double quotes ("
) to user. Ask the user to type in a number.statement
), C# would like to see a semicolon
;
.Console.ReadLine()
waits for the user to type something and then press
enter
. By creating a variable of type String
with
for example, the name textTypedByUser
and then using an assignment (assignment
)
(identified in C# by the =
character), the text typed by the
user typed is stored in that variable.declaration
: you must then put the type
in front of it. After that
This way C# knows when you create a new variable and when you do not.
not.String
is a chain of characters. To start computing with it, we have to C#
tell it that we want to convert the value to an int
(for
an integer, also called integer
). We do that by using the method
Convert.ToInt32()
and passing in the parentheses the value
that we want to convert. The result (which we can assign to a variable of type
int
) is an integer: int numberTypedByUser =
Convert.ToInt32(textTypedByUser);
.a
and b
we can create a
new integer int c;
and assign the value of a + b
to it.integer
value back to a String
to put it on the screen.Here C# code showing the concepts discussed above:
Console.WriteLine("Dear user,");
Console.WriteLine("Please type a number (and press enter)");
String textTypedByUser = Console.ReadLine();
Console.WriteLine("You have typed: "+ textTypedByUser);
int numberTypedByUser = Convert.ToInt32(textTypedByUser);
int a = 42;
int b = 365;
int c = a + b;
String answer = Convert.ToString(c);
Console.WriteLine(answer);
It is possible to write this shorter, but keep an eye on whether it remain readable!
With this knowledge, it is possible to create the aforementioned calculator: The user can type in integers. For example, you can choose to tell the tell the user both what the sum and the product of the numbers are.
Did you succeed? Then you have now written yourself a program that can do more than yourself (multiply the numbers 7225 and 5588 within a millisecond, for example) and have taken the first step toward becoming an experienced software engineer.
Stuck? Ask a question of your neighbor! If you can’t figure it out together ask your teacher. In the beginning, this programming can be quite difficult.
If the calculator works then you can program the last 2 requirements. These are:
double
instead of int
.Maybe you already had it, then you are not only good at programming, but you also passed the analysis phase honorably.
Discuss assignments regularly with your teacher and then enter feedback into Feedpulse.
To learn it really well, it is good to make extensions to the assignments, and ask for feedback on these as well. This can lead to a higher grade (note: you have to be able to program it yourself, code-copy from the Internet is not enough). Some possible extensions: