C# 在我的猜谜游戏中创建循环
如果玩家答错了,我如何重复这个代码/游戏C# 在我的猜谜游戏中创建循环,c#,C#,如果玩家答错了,我如何重复这个代码/游戏 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random RandomGenerator = new Random();// random number generator
Console.WriteLine("i have thought of a number between 1 and 100");//display message
//
//
int IN_RandomNum = RandomGenerator.Next(1, 100); //the range
//loop 10 times
for (int i = 0; i < 10; i++) //for loop created
{
int IN_Guess; //guessing integer
Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has
//
IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number
//Now check if guess is same as generated
if (IN_Guess == IN_RandomNum)//if guess is equal to generated number
{
Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly
break; //breaking code
}
else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number
{
Console.WriteLine("Too high");// if guessed number is too high
}
else //then...
{
Console.WriteLine("Too low"); // if guessed number is too low
} if (i==8) //on last turn display this message
{
Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message
}
}
Console.WriteLine("please press enter to quit"); //display message
Console.ReadLine();//keeps application open until enter button hit
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Random RandomGenerator=new Random();//随机数生成器
Console.WriteLine(“我想到了一个介于1和100之间的数字”);//显示消息
//
//
int IN_RandomNum=RandomGenerator.Next(1100);//范围
//循环10次
for(int i=0;i<10;i++)//for创建的循环
{
int IN_Guess;//猜测整数
Console.Write(“{0}左转,输入你的下一个猜测>”,10-i);//玩家的圈数
//
IN_Guess=Convert.ToInt32(Console.ReadLine());//字符串到数字
//现在检查猜测是否与生成的相同
if(IN_Guess==IN_RandomNum)//如果Guess等于生成的数字
{
WriteLine(“在{0}圈内正确,i+1);//如果猜测正确
break;//中断代码
}
else if(IN_Guess>IN_RandomNum)//if Guess高于生成的数字
{
Console.WriteLine(“太高”);//如果猜测的数字太高
}
否则//。。。
{
Console.WriteLine(“过低”);//如果猜测的数字过低
}如果(i==8)//在最后一次转弯时显示此消息
{
Console.WriteLine(“*您只剩下一个猜测!*”;//显示此消息
}
}
Console.WriteLine(“请按enter键退出”);//显示消息
Console.ReadLine();//在按下enter按钮之前保持应用程序打开
}
}
}
请帮忙,因为我真的不知道把while循环放在哪里…我已经没有主意了你需要把所有东西都放在while循环中 当它退出你所拥有的for循环时,不管他们赢还是输,你都会回到顶部重新开始,生成一个新的随机数 在阅读下面的代码之前,请查看我上面的建议并亲自尝试,看看是否可以破解它 大概是这样的:
static void Main(string[] args)
{
Random RandomGenerator = new Random();// random number generator
while (true)
{
Console.WriteLine("i have thought of a number between 1 and 100");//display message
bool guessedCorrect = false;
int IN_RandomNum = RandomGenerator.Next(1, 100); //the range
//loop 10 times
for (int i = 0; i < 10; i++) //for loop created
{
int IN_Guess; //guessing integer
Console.Write("{0} turns left, enter your next guess>", 10 - i);//number of turns players has
//
IN_Guess = Convert.ToInt32(Console.ReadLine()); //string to number
//Now check if guess is same as generated
if (IN_Guess == IN_RandomNum)//if guess is equal to generated number
{
Console.WriteLine("correct in {0} turns", i + 1); //if guessed correctly
guessedCorrect = true;
break; //breaking code
}
else if (IN_Guess > IN_RandomNum) //if guess is higher than generated number
{
Console.WriteLine("Too high");// if guessed number is too high
}
else //then...
{
Console.WriteLine("Too low"); // if guessed number is too low
}
if (i == 8) //on last turn display this message
{
Console.WriteLine("*YOU ONLY HAVE 1 GUESS LEFT!*"); //display this message
}
}
if (guessedCorrect)
{
Console.WriteLine("Good job... Lets try again");
}
else
{
Console.WriteLine("Better luck next timer... here we go");
}
}
}
static void Main(字符串[]args)
{
Random RandomGenerator=new Random();//随机数生成器
while(true)
{
Console.WriteLine(“我想到了一个介于1和100之间的数字”);//显示消息
布尔猜测正确=错误;
int IN_RandomNum=RandomGenerator.Next(1100);//范围
//循环10次
for(int i=0;i<10;i++)//for创建的循环
{
int IN_Guess;//猜测整数
Console.Write(“{0}左转,输入你的下一个猜测>”,10-i);//玩家的圈数
//
IN_Guess=Convert.ToInt32(Console.ReadLine());//字符串到数字
//现在检查猜测是否与生成的相同
if(IN_Guess==IN_RandomNum)//如果Guess等于生成的数字
{
WriteLine(“在{0}圈内正确,i+1);//如果猜测正确
猜测正确=正确;
break;//中断代码
}
else if(IN_Guess>IN_RandomNum)//if Guess高于生成的数字
{
Console.WriteLine(“太高”);//如果猜测的数字太高
}
否则//。。。
{
Console.WriteLine(“过低”);//如果猜测的数字过低
}
如果(i==8)//在最后一次转弯时显示此消息
{
Console.WriteLine(“*您只剩下一个猜测!*”;//显示此消息
}
}
如果(猜测正确)
{
WriteLine(“干得好……让我们再试一次”);
}
其他的
{
Console.WriteLine(“祝你下次好运……我们开始了”);
}
}
}
另外,我每次尝试都失败了,你的游戏太难了:-(你能描述一下你的问题吗?你已经有了for循环。它应该足够了。把你的整个代码放在另一个循环中,比如
while(1)
或while(someCondition)