Im trying to use a try catch block But im facing some problems.. please help
this is the code and the error which Im getting is Error 1 The name 'Program' does not exist in the current context
using System;
namespace AddMinusDivideMultiply
{
class Program
{
public static int i, j;
public static void Main()
{
try
{
Console.Write("Please Enter The First Number :");
string temp = Console.ReadLine();
i = Int32.Parse(temp);
Console.Write("Please Enter The Second Number :");
temp = Console.ReadLine();
j = Int32.Parse(temp);
}
catch (Exception e)
{
Console.WriteLine(" An Execption was thrown: {0}", e.Message);
}
Terms.Minus();
}
}
}
class Terms
{
public static void Add()
{
开发者_Python百科 int add;
add = Program.i + Program.j;
Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
}
public static void Minus()
{
int minus;
minus = Program.i - Program.j;
Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
}
}
Please try AddMinusDivideMultiply.Program
instead. However strange, you are already in the correct namespace.
You currently have a closing brace, making the Terms class outside of the AddMinusDivideMultiply namespace
Try this
using System;
namespace AddMinusDivideMultiply
{
class Program
{
public static int i, j;
public static void Main()
{
try
{
Console.Write("Please Enter The First Number :");
string temp = Console.ReadLine();
i = Int32.Parse(temp);
Console.Write("Please Enter The Second Number :");
temp = Console.ReadLine();
j = Int32.Parse(temp);
}
catch (Exception e)
{
Console.WriteLine(" An Execption was thrown: {0}", e.Message);
}
Terms.Minus();
}
}
class Terms
{
public static void Add()
{
int add;
add = Program.i + Program.j;
Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
}
public static void Minus()
{
int minus;
minus = Program.i - Program.j;
Console.WriteLine("The Subraction Of The First and The Second Number is {0}", minus);
}
}
}
Your braces are not balanced, there are two braces closing the Main method where you want one.
精彩评论