我试图使用try catch块但我面临一些问题..请帮助
这是我得到的代码和错误 错误1当前上下文中不存在名称“程序”
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);
}
}
答案 0 :(得分:1)
请尝试AddMinusDivideMultiply.Program
。不过很奇怪,你已经在正确的命名空间。
答案 1 :(得分:1)
你目前有一个右大括号,使条款类在AddMinusDivideMultiply名称空间之外
试试这个
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);
}
}
}
答案 2 :(得分:0)
你的大括号不平衡,有两个大括号关闭你想要的主方法。