Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# g待编写 writeline(“这是正确的,数字是{0},outputnumber); } } //应该是当(真实的); }而(真=假); } }_C#_Linq_Oop_Random - Fatal编程技术网

C# g待编写 writeline(“这是正确的,数字是{0},outputnumber); } } //应该是当(真实的); }而(真=假); } }

C# g待编写 writeline(“这是正确的,数字是{0},outputnumber); } } //应该是当(真实的); }而(真=假); } },c#,linq,oop,random,C#,Linq,Oop,Random,}您的代码中有很多错误,但我将通过在它们旁边写注释来回顾我看到的主要错误 Random generator = new Random(); bool truth = true; //extra semicolon on next line, //doesn't make sense to convert a boolean to get your initial max range, //just give it an initial value, perhaps 0 int MaxRang

}

您的代码中有很多错误,但我将通过在它们旁边写注释来回顾我看到的主要错误

Random generator = new Random();
bool truth = true;

//extra semicolon on next line, 
//doesn't make sense to convert a boolean to get your initial max range,
//just give it an initial value, perhaps 0
int MaxRange = Convert.ToInt32(truth); ;
int userguess = 0;

//the generator can't get the correct outputnumber yet because MaxRange hasn't
//been set by the user's input yet
int outputnumber = generator.Next(MaxRange);

do
{
    //this won't get the user input because the parameters in WriteLine() 
    //that come after the string are for inserting into the string, not recieving input
    Console.WriteLine(" Enter a max number you want to guess from!", MaxRange);

    //need to set Console.ReadLine() to a variable for it to be saved
    //should be MaxRange = Console.ReadLine(); (though that will throw an error if the user inputs anything except numbers)
    Console.ReadLine();

    //outputnumber hasn't been set to a true generated number yet
    Console.WriteLine("please make a guess between 1 and {0}", outputnumber);

    //outputnumber could be renamed to be more clear, I would suggest randomNumber
    //userguess is still 0 during the first loop, need to get the user's guess before comparing to the random number
    //also, could simplify these if/else statements to just a couple of them
    //you're also going to need a while loop somewhere in here to continue having the user
    //guess until they get it right
    if (userguess != outputnumber)
    {
        //should use Console.ReadLine()
        userguess = Convert.ToInt32(Console.Read());

        if (userguess < outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        if(userguess > outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        else if (userguess == outputnumber)
        {
//not indented correctly, missing correct capitalization and missing the right quotes on the string to be wrote
console.writeline("That is correct, the number is {0}, outputnumber);
        }
    }
//should just be while (true);
} while (truth == false);
}

}
Random generator=new Random();
布尔真理=真;
//在下一行加上分号,
//将布尔值转换为初始最大范围是没有意义的,
//给它一个初始值,也许是0
int MaxRange=Convert.ToInt32(真值);
int userguess=0;
//生成器尚未获取正确的输出编号,因为MaxRange尚未获取
//是否已由用户的输入设置
int outputnumber=生成器.Next(最大范围);
做
{
//这将无法获取用户输入,因为WriteLine()中的参数
//字符串后面的用于插入字符串,而不是接收输入
WriteLine(“输入您想从中猜出的最大值!”,MaxRange);
//需要将Console.ReadLine()设置为要保存的变量
//应为MaxRange=Console.ReadLine();(但如果用户输入除数字以外的任何内容,则会引发错误)
Console.ReadLine();
//outputnumber尚未设置为真实生成的数字
WriteLine(“请在1和{0}之间进行猜测”,outputnumber);
//outputnumber可以重命名为更清晰,我建议使用randomNumber
//userguess在第一个循环中仍然为0,在与随机数进行比较之前,需要获得用户的猜测
//此外,可以将这些if/else语句简化为几个
//您还需要在这里的某个地方进行一个while循环,以继续使用该用户
//猜猜看,直到他们猜对为止
if(userguess!=outputnumber)
{
//应该使用Console.ReadLine()
userguess=Convert.ToInt32(Console.Read());
if(userguessoutputnumber)
{
Console.WriteLine(“这不对,再猜一次”);
Console.ReadLine();
}
else if(userguess==outputnumber)
{
//缩进不正确,缺少正确的大写字母和要写入的字符串上缺少正确的引号
writeline(“这是正确的,数字是{0},outputnumber);
}
}
//应该只是暂时(真的);
}而(真=假);
}
}

}谢谢大家!我做了一些小的调整,现在几乎完美了。我仍然得到的唯一错误是,当成功运行它时,会询问用户是否希望再次播放。如果他们说是,游戏将使他们从先前选择的最大值开始玩,而不是输入新值

命名空间Trythisagain { 班级计划 {

public static int SelectedNumber=0;
public static Random ran=new Random();
公共静态bool GameOver=false;
公共静态int UserMaxValue=0;
静态void Main(字符串[]参数)
{
int用户号;
SelectedNumber=ran.Next(0,UserMaxValue);
做
{
WriteLine(“输入您想从中猜出的最大值!”);
UserMaxValue=Convert.ToInt32(Console.ReadLine());
做
{
WriteLine(“选择一个介于1和{0}之间的数字!”,UserMaxValue);
UserNumber=Convert.ToInt32(Console.ReadLine());
猜数(UserNumber);
}while(GameOver==false);
}while(GameOver==false);
}
公共静态无效猜测编号(int UserNumber)
{
int=0;
if(UserNumber所选号码)
控制台。WriteLine(“您的号码错了!请再试一次!”);
其他的
{
Console.WriteLine(“耶!你的号码对了!按1再次播放,按2退出”);
playreach=Convert.ToInt32(Console.ReadLine());
while(再次播放!=1&&playreach!=2)
{
Console.WriteLine(“请仅选择1重新播放或选择2退出!”);
playreach=Convert.ToInt32(Console.ReadLine());
}
如果(再次播放。等于(2))
GameOver=true;
其他的
SelectedNumber=ran.Next(0,UserMaxValue);
}
}
}

}谢谢大家!我做了一些小的调整,现在几乎完美了。我仍然得到的唯一错误是,当成功运行它时,会询问用户是否希望再次播放。如果他们说是,游戏将使他们从先前选择的最大值开始玩,而不是输入新值

命名空间Trythisagain { 班级计划 {

public static int SelectedNumber=0;
public static Random ran=new Random();
公共静态bool GameOver=false;
公共静态int UserMaxValue=0;
静态void Main(字符串[]参数)
{
int用户号;
SelectedNumber=ran.Next(0,UserMaxValue);
做
{
WriteLine(“输入您想从中猜出的最大值!”);
UserMaxValue=Convert.ToInt32(Console.ReadLine());
做
{
WriteLine(“选择一个介于1和{0}之间的数字!”,UserMaxValue);
UserNumber=Convert.ToInt32(Console.ReadLine());
猜数(UserNumber);
}while(GameOver==false);
}while(GameOver==false);
}
公共静态无效猜测编号(int UserNumber)
{
int=0;
if(UserNumber所选号码)
控制台。WriteLine(“您的号码错了!请再试一次!”);
其他的
{
控制台。WriteLin
// loop 1
// get users expected max range
//   if not int
//      display message about invalid input
//   if int
//      break loop 1 to start guessing
// end loop 1
// generate random number for user to guess
// loop 2
// prompt user for their guess
// parse input and verify int
//   if not int
//      display message about invalid input
//      continue loop 2 skipping eval code to reprompt for input
// evaluate their guess
//   if matches
//      show success message
//      break loop 2
//   else
//      show failure message
Random generator = new Random();
bool truth = true;

//extra semicolon on next line, 
//doesn't make sense to convert a boolean to get your initial max range,
//just give it an initial value, perhaps 0
int MaxRange = Convert.ToInt32(truth); ;
int userguess = 0;

//the generator can't get the correct outputnumber yet because MaxRange hasn't
//been set by the user's input yet
int outputnumber = generator.Next(MaxRange);

do
{
    //this won't get the user input because the parameters in WriteLine() 
    //that come after the string are for inserting into the string, not recieving input
    Console.WriteLine(" Enter a max number you want to guess from!", MaxRange);

    //need to set Console.ReadLine() to a variable for it to be saved
    //should be MaxRange = Console.ReadLine(); (though that will throw an error if the user inputs anything except numbers)
    Console.ReadLine();

    //outputnumber hasn't been set to a true generated number yet
    Console.WriteLine("please make a guess between 1 and {0}", outputnumber);

    //outputnumber could be renamed to be more clear, I would suggest randomNumber
    //userguess is still 0 during the first loop, need to get the user's guess before comparing to the random number
    //also, could simplify these if/else statements to just a couple of them
    //you're also going to need a while loop somewhere in here to continue having the user
    //guess until they get it right
    if (userguess != outputnumber)
    {
        //should use Console.ReadLine()
        userguess = Convert.ToInt32(Console.Read());

        if (userguess < outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        if(userguess > outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        else if (userguess == outputnumber)
        {
//not indented correctly, missing correct capitalization and missing the right quotes on the string to be wrote
console.writeline("That is correct, the number is {0}, outputnumber);
        }
    }
//should just be while (true);
} while (truth == false);
}

}
    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("Enter a max number you want to guess from!");
            UserMaxValue = Convert.ToInt32(Console.ReadLine());

        do
            {


                Console.WriteLine("Select a number 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("Your Number is Wrong! Please try Again!");
        else if (UserNumber > SelectedNumber)
            Console.WriteLine("Your Number is Wrong! Please Try Again!");
        else
        {

            Console.WriteLine("Yay! You got the right number! Press 1 to play again press 2 to quit");
            playagain = Convert.ToInt32(Console.ReadLine());

            while (playagain != 1 && playagain != 2)

            {
                Console.WriteLine("Please Only Select 1 to play again or 2 to quit!");
                playagain = Convert.ToInt32(Console.ReadLine());

            }

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

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