C#中Switch案例出错 [英] Error in the Switch case in C#
问题描述
Hello Experts,
我在这里做错了吗?我想我可能在while循环中遇到问题,因为所有的Console.WriteLine commang在该行之后用红色加下划线。
Hello Experts,
Am i doing something wrong here pls? I think i might have a problem in the while loop since all the Console.WriteLine commangs are underlined in red after that line.
namespace myDemo
{
class Program
{
static void Main(string[] args)
{
int option;
bool state = true;
public int Option
{
get
{
return option;
}
set
{
if (value == 0 || value > 16)
{
Console.WriteLine("Invalid Option");
}
else
{option = value;}
}
}
while(state = true)
{
Console.WriteLine("Please Enter the Ocupation");
Console.WriteLine("1. Director");
Console.WriteLine("2. Councilor");
Console.Clear();
Option = Console.ReadLine();
switch(int Option)
{
case 1:
{
Console.WriteLine("Displaying Director Options:");
Console.WriteLine("2. Display Students Details:");
Console.WriteLine("3. Display Councilors Details: ");
Console.WriteLine("4. Display Visits Details:");
Console.WriteLine("5. Add Student:");
Console.WriteLine("6. Remove Student:");
Console.WriteLine("7. Add Councilor:");
Console.WriteLine("8. Remove Councilor");
Console.WriteLine("9. Change Councilor Available Hours");
Console.WriteLine("10. Add Visit");
}
}
}
}
}
}
< br $> b $ b
屏幕截图: http://s2.postimage.org/osh5yy9op/Capture.jpg [ ^ ]
推荐答案
您好,
更改
Hi,
Change
switch(int Option)
进入:
into:
switch (Option)
另外,更改
Also, change
case 1:
{
Console.WriteLine("Displaying Director Options:");
Console.WriteLine("2. Display Students Details:");
Console.WriteLine("3. Display Councilors Details: ");
Console.WriteLine("4. Display Visits Details:");
Console.WriteLine("5. Add Student:");
Console.WriteLine("6. Remove Student:");
Console.WriteLine("7. Add Councilor:");
Console.WriteLine("8. Remove Councilor");
Console.WriteLine("9. Change Councilor Available Hours");
Console.WriteLine("10. Add Visit");
}
进入:
into:
case 1:
Console.WriteLine("Displaying Director Options:");
Console.WriteLine("2. Display Students Details:");
Console.WriteLine("3. Display Councilors Details: ");
Console.WriteLine("4. Display Visits Details:");
Console.WriteLine("5. Add Student:");
Console.WriteLine("6. Remove Student:");
Console.WriteLine("7. Add Councilor:");
Console.WriteLine("8. Remove Councilor");
Console.WriteLine("9. Change Councilor Available Hours");
Console.WriteLine("10. Add Visit");
break;
你不需要使用括号。不要忘记 break;
在开关结束前的下一个
。 case
之前
在此处阅读更多关于开关
:
http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.110 %29.aspx [ ^ ]
另外,更改此:
You don''t need to use brackets. Don''t forget break;
before a next case
of before the end of the switch
.
Read more about switch
here:
http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.110%29.aspx[^]
Also, change this:
Option = Console.ReadLine();
到此:
into this:
try
{
Option = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Input string isn't in a correct format.");
continue; // use 'continue' only in a loop
}
对不起,但是在看到之后我发现了一些错误你的屏幕截图。
首先:
I''m sorry, but I found a few mistakes after I saw your screen shot.
First:
public int Option
{
get
{
return option;
}
set
{
if (value == 0 || value > 16)
{
Console.WriteLine("Invalid Option");
}
else
{option = value;}
}
}
您可以在函数中创建属性。那是不可能的。了解有关房产的更多信息:
http:/ /msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx [ ^ ]
第二名:
Don'使用
You create a property INTO a function. That''s not possible. Read more about properties:
http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx[^]
Second:
Don''t use
while (state = true)
使用
Use
while (state == true)
了解有关 == $的更多信息c $ c>运营商:
http://msdn.microsoft.com/ en-us / library / 53k8ybth.aspx [ ^ ]
while (state = true)
总是 true
,你的意思是:
is always true
, you mean:
while (state == true)
鉴于上述内容和您的链接,我认为您应该花一些时间重读C#表达式和语句的参考指南。
Given the above, and your link, I think you should spend some more time rereading your reference guide on C# expressions and statements.
>
这篇关于C#中Switch案例出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!