2014-10-27 89 views
0

case 3:我写了mutteer(ba, bb, bc, bd, be);,它似乎给出错误(名称ba在当前上下文中不存在)。 它给出与bbbcbdbe相同的错误。该名称在当前上下文中不存在

我做错了什么?

我删除了大部分不必要的代码:

static void menu() 
    { 
     int loop = 4; 
     Console.WriteLine(" 3 Mutteer Voorraad"); 

     while (loop > 2) 
      { 
      var ans = Console.ReadLine(); 
      mp3(); 
      int choice = 0; 
      if (int.TryParse(ans, out choice)) 
      { 
       switch (choice) 
       { 
        case 3: 
         Console.WriteLine("Mutteer Voorraad."); 
         mutteer(ba, bb, bc, bd, be); 
         break; 

        default: 
         Console.WriteLine("Wrong selection!!!"); 
         Thread.Sleep(1800); 
         Console.Clear(); 
         goto top; 
       } 
      } 
      else 
      { 
       Console.WriteLine("You must type numeric value only!!!"); 
       Thread.Sleep(1800); 
       Console.Clear(); 
       goto top; 
      } 
     } 
    } 

static void mp3() 
    { 
     int ba = 500; 
     int bb = 500; 
     int bc = 500; 
     int bd = 500; 
     int be = 500; 

     mp3players mp1 = new mp3players(); 
     mp1.id = 1; 
     mp1.VR = ba; 
    } 

static void mutteer(int ba, int bb, int bc, int bd, int be) 
    { 
     int i = 1; 
     int a; 

     startloop: 
     while (i == 1) 
      { 
       Console.WriteLine("Wat is het Id van de mp3 speler?"); 
       try 
       { 
        a = Convert.ToInt16(Console.ReadLine()); 
       } 
       catch 
       { 
        Console.WriteLine("Dat is geen nummer waarde!"); 
        goto startloop; 
       } 

       if (a == 1) 
       { 
        Console.WriteLine("Wat is het nieuwe voorraad"); 
        try 
        { 
         i = 2; 
         ba = Convert.ToInt32(Console.ReadLine()); 
        } 
        catch 
        { 
         Console.WriteLine("Dat is geen nummer waarde!"); 
         goto startloop; 
        } 
      } 
     } 
+0

错误正是消息所说的。在你使用它的时候'ba'没有任何意义。 – 2014-10-27 03:04:05

+0

我做了一个快速编辑,也许这改变了你说的话,如果没有,我想问我是如何做到的,所以它有一个意思。 – yuki 2014-10-27 03:09:25

+0

如果你是C#的新手,那么请阅读变量的范围。 http://msdn.microsoft.com/en-IN/library/aa691132(v=vs.71).aspx – dotnetstep 2014-10-27 03:20:09

回答

1

您需要的ba的声明和其他变量移动到全球范围。由于代码当前已编写,因此只能在mp3方法的范围内访问它们。您应该有:

static int ba = 500; 
static int bb = 500; 
static int bc = 500; 
static int bd = 500; 
static int be = 500; 

    static void mp3() 
    { 
    mp3players mp1 = new mp3players(); 
    mp1.id = 1; 
    mp1.VR = ba; 
    } 
相关问题