Perform
In this stage we’ll finish coding the planned features of FitnessFrog. We’ll learn many important programming constructs.
Now, we want to add the user's entry into another entry. Try the code below.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
string entry;
// Prompt the for minutes exercised
Console.Write("Enter how many minutes you exercised: ");
entry = Console.ReadLine();
entry = entry + 22;
// Add minutes exercised to a running total
// Display the running total in minutes
Console.Write("You entered " + entry + " minutes");
// Ask repeatedly until user quits
}
}
}
What do you get?
Yes, it is because our entry
variable is String. We cannot perform addition, substraction, division, multiplication to string. We need to convert it to integer.
int minutes = int.Parse(entry);
Now, try it again.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
string entry;
// Prompt the for minutes exercised
Console.Write("Enter how many minutes you exercised: ");
entry = Console.ReadLine();
int minutes = int.Parse(entry);
minutes = minutes + 22;
// Add minutes exercised to a running total
// Display the running total in minutes
Console.Write("You entered " + minutes + " minutes");
// Ask repeatedly until user quits
}
}
}
How if we have +, -, *, /, %
in one line? who performed first?
Like we learn in school, multiplication (*), division (/), and modulus (%) are performed first. Followed by addition (+) and subtraction (-).