I just started C# and I've been stuck on this for over an hour. So I'm trying to make a program to calculate age and this is the code I thought would work:
using System;
using System.Collections.Generic;
namespace Project1
{
class Program
{
static void Main(string[] args)
{
int x, y, age;
Console.Write("Current year = ");
x = int.Parse(Console.ReadLine());
Console.Write("Your birth date = ");
y = int.Parse(Console.ReadLine());
age = x - y
Console.Write("Your are " age " years old!");
Console.ReadKey();
}
}
}
I expected to see something like this in the console:
Current year = 2023 Your birth date = 2004 You are 19 years old!
But instead I get an error each time and it highlights the lines of code in which I tried to assign x & y, it also underlined Consol.ReadLine()
IDE: Microsoft Visual Studio
CodePudding user response:
All syntax errors corrected:
using System; // u instead of U
using System.Collections.Generic; // u instead of U
namespace Project1 // n instead of N
{
class Program // c instead of C
{
static void Main(string[] args)
{
{
{
int x, y, age;
Console.Write("Current year = "); // missed `;`
x = int.Parse(Console.ReadLine());
Console.Write("Your birth date = "); // missed `.`
y = int.Parse(Console.ReadLine());
age = x - y; // missed `;`
Console.Write("Your are " age " years old!"); // missed `"` and `;`
Console.ReadKey();
}
}
}
} // missed `}`
} // missed `}`
Please, remember:
- It is you who reads the program first, format it
- VS is your friend, it provides hints what is wrong with the syntax
Let's improve your code, it can be something like this:
using System;
namespace Project1 {
class AgeComputing {
static void Main(string[] args) {
Console.Write("Current year = ");
int currentYear = int.Parse(Console.ReadLine());
Console.Write("Your birth date = ");
int birthYear = int.Parse(Console.ReadLine());
int age = currentYear - birthYear;
Console.Write($"Your are {age} years old!");
Console.ReadKey();
}
}
}
CodePudding user response:
A syntax error:
using System;
using System.Collections.Generic;
namespace Project1
{
class Program
{
static void Main(string[] args)
{
int x, y, age;
Console.Write("Current year = ");
x = int.Parse(Console.ReadLine());
Console.Write("Your birth date = ");
y = int.Parse(Console.ReadLine());
age = x - y; //u missed a ;
Console.Write("Your are " age " years old!");
Console.ReadKey();
}
}
}