2016-09-30 158 views
0

我目前正在学校随机数猜测游戏。这只是我第二周的节目,所以这项任务对我来说至少是一个挑战。我想我已经把所有东西都放下了,除了我的循环必须在某个地方被打破(我找不到它!),因为它从来没有识别出正确的答案 - 只是指出每个猜测都是错误的。我做了什么??我希望你能帮助我:)非常感谢!麻烦猜测数字游戏在C#

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

namespace Assn3_2 
{ 
class Program 
{ 
    public static int SelectedNumber = 0; 
    public static Random ran = new Random(); 
    public static bool GameOver = false; 
    public static int UserMaxValue = 0; 



    static void Main(string[] args) 
    { 
     int UserNumber; 
     SelectedNumber = ran.Next(0, UserMaxValue); 

     do 
     { 
      Console.WriteLine("What is the maximum number you want to guess from?"); 
      UserMaxValue = Convert.ToInt32(Console.ReadLine()); 

      do 
      { 

       Console.WriteLine("Please make a guess between 1 and {0}", UserMaxValue); 
       UserNumber = Convert.ToInt32(Console.ReadLine()); 
       GuessNumber(UserNumber); 

      } while (GameOver == false); 
     } while (GameOver == false); 
    } 

    public static void GuessNumber(int UserNumber) 

    { 
     int playagain = 0; 

     if (UserNumber < SelectedNumber) 
      Console.WriteLine("Wrong. Please try again."); 
     else if (UserNumber > SelectedNumber) 
      Console.WriteLine("Wrong.Please try again."); 
     else 
     { 
      Console.WriteLine("Correct! The number is {0}. Again? (1 or 2)"); 
      playagain = Convert.ToInt32(Console.ReadLine());; 

      while (playagain != 1 && playagain !=2) 
      { 
       Console.WriteLine("Invalid answer. Again? Y or N"); 
       playagain = Convert.ToInt32(Console.ReadLine()); 
      } 

      if (playagain.Equals(2)) 
       GameOver = true; 

      else 
       SelectedNumber = ran.Next(0, UserMaxValue); 
     } 


    } 
} 


} 
+3

好的,你写了一些代码。现在,该调试它了。设置断点,检查变量并观察代码逐行执行。 – Plutonix

+0

我想它一定是错误的。 –

回答

1

此:

SelectedNumber = ran.Next(0, UserMaxValue); 

运行此之前您提示用户在UserMaxValue值来填充,这样有效地你做

SelectNumber = ran.Next(0, 0); 

不是很随意,当只有一个选择,并且该选择是0 ...尤其是当您只允许在1和t之间的猜测他以后得到的最大值。

+0

非常感谢Marc B!我做了这个改变,并创建了一个除“0”以外的随机数! 现在,我唯一的问题是不是循环回到我的第一个'do'语句(设置最大值),我的程序只是要求用户在相同的值范围内进行猜测。有没有人有任何建议或提示如何改变? –

+0

@VictoriaN这个问答是否会重复,直到您修复所有错误?请一次提出一个问题.... –

+0

而这不是一项调试服务。 ** MOST **学习如何编码的重要部分是学习如何调试。如果你甚至不想自己想办法,你什么都不会学。 –