日期:2014-05-18  浏览次数:20951 次

C#类
namespace Ti2
{
  class ZhengXing 
  {
  private int a;
  public ZhengXing(int x)
  {
  a = x;
  }
  public bool ZhiShu()
  {
  bool flag=true;
  for (int i=2;i<a;i++)
  if(a%i==0)
  {
  flag =false ;
  break;
  }
  return flag;
  }
  public int ChiFang(int N)
  {
  int x=1;
  if(N ==0)
  x=1;
  else 
  for(int i=0;i <N;i++)
  x=x*a;
  return x;
  }
  public void ZhuanHuan()
  {
  int count = 0;
  int[] b;
  for (int i = 0; a != 0; i++)
  {
  b[i] = a % 2;
  a = a / 2;
  count = count + 1;
  }
  for (int i = count - 1; i >= 0; i--)
  Console.Write(b[i]);
  }
  }
  class Program
  {
  static void Main(string[] args)
  {
  ZhengXing ZX = new ZhengXing(10);
  bool flag = ZX.ZhiShu();
  if (flag == true)
  Console.WriteLine("这个数是质数");
  else
  Console.WriteLine("这不是一个质数");
  int x = ZX.ChiFang(2);
  Console.WriteLine("这个数的N次方是{0}",x);
  ZX.ZhuanHuan();
  Console.ReadLine();
  }
   
  }
}

错误 1 使用了未赋值的局部变量“b”
(这个错是什么意思啊,求解释啊,先解答就得全分哦,我是新手《《《,谢谢)


------解决方案--------------------
public void ZhuanHuan()
{
int count = 0;
List<int> b = new List<int>();
for (int i = 0; a != 0; i++)
{
b.Add(a % 2);
a = a / 2;
count = count + 1;
}
for (int i = count - 1; i >= 0; i--)
Console.Write(b[i]);
}

------解决方案--------------------
int[] b; //b只是定义了,没有初始化
------解决方案--------------------
你就是没给数组int[] b初始化,一般的做法是下面这样(也许C语言时候是这样的)
C# code

 public void ZhuanHuan()
            {
                int count = 0;
                int[] b = new int[100];
                for (int i = 0; a != 0; i++)
                {
                    b[i] = a % 2;
                    a = a / 2;
                    count = count + 1;
                }
                for (int i = count - 1; i >= 0; i--)
                    Console.Write(b[i]);
            }