我運行我的代碼,但是當它進入switch語句時,它不執行它並從程序中退出。 在switch語句中,當用戶按1時,它將進行加法運算,2進行減法運算等等。 我無法理解錯誤。開關語句不起作用。 C#
int c; c = 0;
int a;
int b;
Console.Write Line("Enter First Number");
a = Convert.ToInt16(Console.Read Line());
Console.Write Line("Enter First Number");
b = Convert.ToInt16(Console.Read Line());
Program k = new Program();
k.display();
switch(c)
{
case 1 :
{
Console.Write Line("Answer is {0}",k.add(a,b));
}
break;
case 2:
{
Console.Write Line("Answer is {0}", k.sub(a,b));
}
break;
case 3:
{
Console.Write Line("Answer is {0}", k.prod(a, b));
}
break;
case 4 :
{
Console.Write Line("Answer is {0}", k.divide(a, b));
}
break;
default:
{
Console.Write Line("Enter Valid value");
}
break;
}
Console.Read Key();
}
public void display()
{
Console.Write Line("Menu");
Console.Write Line("1.Add");
Console.Write Line("2.Subtract");
Console.Write Line("3.Multiply");
Console.Write Line("4.Divide");
Console.Write Line("5.Modulus");
}
public int add(int x, int y)
{
int sum;
sum = x + y;
return sum;
}
public int sub(int x, int y)
{
int subtract;
subtract = x - y;
return subtract;
}
public int prod(int x, int y)
{
int p;
p = x * y;
return p;
}
public int divide(int x, int y)
{
int div;
div = x/y;
return div;
}
你要問「C」值之外。總是0 –
你已經設置了'c = 0;'但沒有改變它的值。因此,您的開關箱都不會觸發。 – ChrisF
'switch語句不工作'這是c#的一個已知錯誤:)) – Eser