Professional Documents
Culture Documents
Path Menu Get Unstuck Tools: My Home
Path Menu Get Unstuck Tools: My Home
Path Menu
Connected to Codecademy
Get Unstuck
Tools
Learn
HELLO WORLD
Getting Input
We can also read input from a user. The command Console.ReadLine() captures text that a user
types into the console.
In this example, the program writes a question to the console and waits for user input. Once the
user types something and presses Enter (or Return), the input is printed back out to the user.
input isused in the following line so that the printed message will change based on what the user
types. For example, if you ran the program and responded to the question with 101, then the
value of input would be "101" and You are 101 years old! would be printed to the console.
Instructions
1.
Run the code and answer the question.
dotnet run
in the console and pressing Enter.
Checkpoint 2 Passed
Hint
You’ll have to run the code with dotnet run, then answer the question. By the end your console
should have something like this:
$ dotnet run
How old are you?
16
You are 16 years old!
In terminal:
Type
Dotnet
Output:
How old r u?
Type 53
using System;
namespace GettingInput
class Program
static
}
}
My Home
Path Menu
Workspace restored.
Get Unstuck
Tools
Learn
HELLO WORLD
Comments
Ironically, an essential tool in programming is the ability to tell a computer to ignore a part of a
program. Text written in a program but not run by the computer is called a comment. In C#,
anything after a // or between /* and */ is a comment. In spoken word we call these symbols
“forward slashes” and “asterisks”.
Comments can:
Instructions
1.
Add a comment to the code right above the first Console.WriteLine().
The comment should explain what this program does.
Checkpoint 2 Passed
Hint
A comment must:
begin with // or
be surrounded by /* and */
program.cs
using System;
namespace GettingInput
class Program
}
using System;
namespace BusinessSolution
class Program
Console.WriteLine(netIncome);
Console.WriteLine(breakEvenVolume);
}
My Home
Path Menu
Workspace restored.
Get Unstuck
Tools
Learn
WORKING WITH NUMBERS
Arithmetic Operators
So what can we do with numerical data? A first step is to write expressions using arithmetic
operators.
addition +
subtraction -
multiplication *
division /
We can use these symbols to perform operations on numbers and create new values.
// prints 23
When using operators, it’s important to pay attention to data types. If we use two integers, it will
return an integer every time. However, if we combine an integer with a double, the answer will
be a double. Let’s look at the following example:
Console.WriteLine(5 / 3);
Console.WriteLine(5 / 3.0);
// prints 1
// prints 1.66667
The first operation that we log uses two ints. While 3 doesn’t go into 5 evenly, we are still left
with an int whole number. In the second operation, we use an int and a double, so the final result
is a double.
C# follows the order of operations. If we do 1 + 2 * 3, should the answer be 9 or 7? C# follows a
set of rules to determine which operations to perform first (the answer is 7). It’s good practice to
use parentheses to explicitly tell C# how to calculate these expressions.
Notice in the following example, even if the addition symbol appears like it should come first,
the multiplication operation will happen first.
// prints 35
Instructions
1.
Did you know that your age would be different on another planet? Different planets orbit the sun
at different rates, so 1 year on earth can be much shorter or much longer on another planet,
depending on their position in the solar system.
Hint
Since an age is a whole number, save it to an int variable:
To calculate a person’s age on another planet, we need to know how long it takes each planet to
orbit the Sun (relative to a year on Earth):
Jupiter takes 11.86 years
Create a variable named jupiterYears and save the amount of years it takes to orbit as their
value.
Checkpoint 3 Passed
Hint
Since these values have a decimal point, we’ll want to use the double data type. For example:
Hint
To calculate a person’s age on Venus, for example:
double venusAge = userAge/venusYears;
4.
If we were to fly to Jupiter, it could take as long as 2,242 (Earth) days or about 6.142466 years!
Hint
Since there is a decimal, use a double data type.
5.
When you reach Jupiter:
What would be your age in Earth years? Save your answer to newEarthAge.
What would be your age in Jupiter years? Save your answer to newJupiterAge.
Checkpoint 6 Passed
Hint
We can find our new age on Earth by adding the time it took to get to Jupiter to our current age:
Hint
To log something to the console, use the Console.WriteLine() command.
Example.
using System;
namespace PlanetCalculations
class Program
// Your Age
// Age on Jupiter
// Time to Jupiter
Console.WriteLine(jupiterAge);
Console.WriteLine(newEarthAge);
Console.WriteLine(newJupiterAge);
}
My Home
Path Menu
Connected to Codecademy
Get Unstuck
Tools
Learn
WORKING WITH NUMBERS
Operator Shortcuts
Often we need to update a variable in our program. We can do so by modifying that variable
using an arithmetic expression, then re-saving it to the same variable name:
Instructions
1.
Ever heard of the phrase, “two steps forward, one step back?” It means that you can make
progress in some task, but also might suffer some setbacks. It’s also a great way to illustrate the
concept of incrementing and decrementing!
Next, increment the value of steps by two and resave this new value to steps.
Hint
There are two ways of incrementing a variable:
Console.WriteLine(variableName);
using System;
namespace MakingProgress
class Program
int steps = 0;
steps += 2;
steps--;
Console.WriteLine(steps);