using System;
namespace myComponents
{
public class Quote
{
public static string  ShowQuote()
{
            Random myRand = new Random();
switch(myRand.Next(3))
{
case 0:
return "Look before you leap";
break;
case 1:
return "Necessity is the mother of invention";
break;
case 2:
return "Life is full of risks";
break;
}
}
}
}调用:Quote.ShowQuote();

解决方案 »

  1.   

    Random myRand = new Random();一句是要定义一个全局变量嘛?如果是的,就写成这样:public Random myRand = new Random();
      

  2.   

    你的程序有两个问题.1.第一个,叫"自以为是", myRand.Next(3)以后case 0,1,2, 对于人脑来说,没有什么理解问题,但是对于编译器,它这时候并并不知道,所以当不是(0,1,2,它会这样以为)的时候,你的函数ShowQuote就没有返回值了.要加上default的情况.2. return "Look before you leap";和后面的break;重复了,编译器会认为break这句话没有办法执行到,去了break就可以了.我把完整的代码贴上面,以后要多用"电脑"想问题啊.using System;
    namespace myComponents
    {
    public class Quote
    {
    Random myRand = new Random();
    public string ShowQuote()
    {
    switch(myRand.Next(3))
    {
    case 0:
    return "Look before you leap";

    case 1:
    return "Necessity is the mother of invention";

    case 2:
    return "Life is full of risks";

    default:
    return "ERROR";

    }
    }
    }
    }