2016-02-14 122 views
-1

我在这个错误上花了近一个小时,我似乎无法找到我出错的地方(新的一组眼睛可能会帮助大声笑)。这是{array = Random.genRandom();}我也得到名称rand不存在于我的MainClass的上下文。在那里它说'也为我的构造函数得到一个错误,我基本上只是做一个随机数字生成器游戏,其中的数字显示在开始,人可以得到一个罢工,球等等,对于错误的答案。我有两个班。一个主类和一个子类。System.Random不包含定义.... C#?

MainClass:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace NumBaseBall 
{ 
    class MainClass 
    { 
     static void Main(string[] args) 
     { 
      int[] player; 
      int[] array; 
      int NumberOfTries = 0; 
      int Strikes = 0; 
      int ball = 0; 
      int input; 
      int count = 0; 
      int g = 0; 
      decimal game = 0; 
      decimal wins = 0; 
      decimal percent = 0; 
      int Ru = 0; 



      Console.WriteLine("Welcome to Number Baseball Game"); 
      Console.WriteLine("By: "); 


      while (Ru != -1) 
      { 
       Console.WriteLine("The three Digit random number is: \t"); 

       MethodClass = rand = new MethodClass(); 

       array = Random.genRandom(); 
       Console.WriteLine(); 

       NumberOfTries = 0; 

       game++; 

       while (NumberOfTries < 5) 
       { 

        ball = 0; 
        Strikes = 0; 

        Console.Write("Enter three digit number: \t"); 
        input = Convert.ToInt32(Console.ReadLine()); 

        player = MethodClass.splitNumber(input); 

        for (int i = 0; i < 3; i++) 
        { 
         count = 0; 

         if ((player[i] == array[g]) && (count == 0)) 
         { 
          if (player[0] == array[0]) 
          { 
           Strikes++; 
          } 

          else 
          { 
           ball++; 
          } 
          count++; 

         } 

         else if ((player[i] == array[g + 1]) && (Strikes == 0)) 
         { 
          if (player[2] == array[2]) 
          { 
           Strikes++; 
          } 
          else 
          { 
           ball++; 
          } 
         } 
         if (Strikes == 3) 
         { 
          Console.WriteLine("Goodjob! You guessed the random number!"); 
          wins++; 
          continue; 

         } 
         else if (Strikes == 0 && ball == 0) 
         { 
          Console.WriteLine("0 Strikes, 0 ball"); 
         } 
         else if ((Strikes > 0 && Strikes < 3) || (ball > 0)) 
         { 
          Console.WriteLine("{0} Strikes and {1}", Strikes, ball); 
         } 
         NumberOfTries++; 
        } 
        if (Strikes == 0) 
        { 
         Console.WriteLine("\nYou lost!"); 
        } 
        percent = (wins/game) * 100; 

        Console.WriteLine("Total amount played: {0}, Number of wins: {1}, Winning Percentage: {2:F2}", 
           game, wins, percent); 
        Console.WriteLine("Press Enter to exist"); 
        Console.ReadLine(); 
       } 
      } 

     } 
    } 
} 

MethodsClass:

//Mohamed Shire 



using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace NumBaseBall 
{ 
    class MethodClass 
    { 
     int[] random = new int[3]; 


     public int[] genRandom() 
     { 
      int my; 
      Random rand = new Random();  
      for (int i = 0; i < random.Length; i++) 
      { 
       my = rand.Next(1, 10); 
       if (isExists(random, my) == false) 
       { 
        random[i] = my; 
        Console.Write(random[i]); 
       } 
       else 
       { 
        i--; 
       } 
      } 
      return random; 


     } 

     public bool isExists(int[] array, int my) 
     { 
      for (int i = 0; i < array.Length; i++) 
      { 
       if (array[i] == my) 
       { 
        return true; 

       } 
      } 

      return false; 
     } 

     public static int[] splitNumber(int input) 
     { 
      int[] uInput = new int[3]; 
      uInput[0] = input/100; 
      uInput[1] = input % 100/10; 
      uInput[2] = input % 100 % 10; 
      return uInput; 

     } 

     public static void outputArray(int[] array) 
     { 
      for (int i = 0; i < array.Length; i++) 
      { 
       Console.WriteLine(array[i]); 
      } 

     } 


    } 


} 
+1

请阅读[this](https://stackoverflow.com/help/mcve)并尝试将您的代码修剪为可重现此问题的较小示例。这样做你可能会为自己解决问题。 –

+0

使用['int.TryParse()'](http://stackoverflow.com/a/199484/380384)确保程序顺利流动后,检查“ENTER”。 – ja72

回答

0

尝试修改此:

MethodClass = rand = new MethodClass(); 

array = Random.genRandom(); 

到:

MethodClass rand = new MethodClass(); 

array = rand.genRandom(); 
+0

嘿,只是一个简单的问题:我想要一个提示,当玩家击中0时,游戏重新开始,当他们输入时,它终止。这里是我所拥有的:'Console.Write(“继续播放,点击0:退出游戏点击输入。继续输入除-1之外的任何数字:”);' – JavaStudent22

+0

我知道我可以使用Convert.ToInt32 '为0,但是他们什么时候碰到ENTER退出? – JavaStudent22