Reputation: 11
My coding teacher gave me homework to build b a working code. So i did and when i ran it it said there was an error. I couldn't find one and neither could my teachet. So my mom recommended to ask here to see if maybe we messed something. Sorry if i have spelling mistakes i am still learning English. P.s. i am learning the language c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Dcoder
{
public class Program
{
public static void Main(string[] args)
{
string sibling1;
string sibling2;
int age_sibling1;
int age_sibling2;
Console.WriteLine( " insert your name and your age " );
sibling1 = string Console.ReadLine();
age_sibling1 = int.Parse(Console.ReadLine());
Console.WriteLine( " insert your sibrlings name and age " );
sibling2 = string Console.ReadLine();
age_sibling2 = int.Parse(console.ReadLine());
if (age_sibling1 > age_sibling2);
Console.WriteLine(sibling1 + " is bigger " );
Else;
if (age_sibling2 > age_sibling1);
Console.WriteLine(sibling2 + " is bigger " );
}
}
}
Upvotes: 1
Views: 80
Reputation: 6528
Made some changes to your code. Some suggestions: Int.Parse will throw an exception if user doesnt enter a number. I would suggest using try/catch or Int32.TryParse.
Another thing is if you are using variable1, variable2, variable3 etc. its time to create a method and avoid using variableN.
string sibling1, sibling2;
int age_sibling1, age_sibling2;
Console.WriteLine("Insert sibling 1 name ");
sibling1 = Console.ReadLine();
Console.WriteLine("Insert sibling 1 age ");
age_sibling1 = int.Parse(Console.ReadLine());
Console.WriteLine("Insert sibling 2 name ");
sibling2 = Console.ReadLine();
Console.WriteLine("Insert sibling 2 age ");
age_sibling2 = int.Parse(Console.ReadLine());
if (age_sibling1 > age_sibling2)
Console.WriteLine($"{sibling1} is bigger ");
else
Console.WriteLine($"{sibling2} is bigger ");
// Wait for user.
Console.ReadKey();
Upvotes: 1