
C# 猜数字
Windows应用程序游戏规则:计算机随机产生一个不重复的四位数,玩家输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同且数字相同,那么将会是1A,...
Windows应用程序
游戏规则:计算机随机产生一个不重复的四位数,玩家输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同且数字相同,那么将会是1A,如果数字相同而位置不同,将会显示1B。如:计算机的随机数字为:1234 ,猜的数字为:1354 ,此时计算机会提示为:2A1B,2A表示在这次猜测中,有二个数字及其位置都正确,1B表示有一个数字正确但位置不正确。
题目是这样的,请大大们提供一下输入数字以后点击按钮后执行的程序代码并注释,我是初学者,还请各位大大们多多帮助。
我最先也是采用每位随机生成一个一位数字的方法 但是确实是 麻烦...所以后来就放弃了 现在输出又遇到问题...好菜... 展开
游戏规则:计算机随机产生一个不重复的四位数,玩家输入四位不重复的数与计算机给出的数作对比,如果与计算机给出的数的位置相同且数字相同,那么将会是1A,如果数字相同而位置不同,将会显示1B。如:计算机的随机数字为:1234 ,猜的数字为:1354 ,此时计算机会提示为:2A1B,2A表示在这次猜测中,有二个数字及其位置都正确,1B表示有一个数字正确但位置不正确。
题目是这样的,请大大们提供一下输入数字以后点击按钮后执行的程序代码并注释,我是初学者,还请各位大大们多多帮助。
我最先也是采用每位随机生成一个一位数字的方法 但是确实是 麻烦...所以后来就放弃了 现在输出又遇到问题...好菜... 展开
8个回答
展开全部
public string Compare(string computerString, string playerString)
{
List<char> player = new List<char>();
int index = 0;
foreach (char item in playerString)
{
player.Add(item);
index++;
}
int aCount = 0;
int bCount = 0;
index = 0;
foreach (char item in computerString)
{
if (item == player[index])
aCount++;
else if (player.Contains(item))
bCount++;
index++;
}
return string.Format("{0}A{1}B", aCount, bCount);
}
不要激动,答案给你了,我没有办法调试,你自己试一下,我不能保证绝对正确.哪句不理解再问.
虽然输入的是数字,但是不一定非要把东西当作数字来处理,
比如随机4个不重复的数字,
Random 方法有个参数可以指定取值范围,
我们先把0~9 push到一个集合中.比如List<int>
然后我们第一次随机0~9之间的数字,把这个数字当作索引在List中取值然后从List中删除,第二次我们随机0~8之间的数字,以此类推.
{
List<char> player = new List<char>();
int index = 0;
foreach (char item in playerString)
{
player.Add(item);
index++;
}
int aCount = 0;
int bCount = 0;
index = 0;
foreach (char item in computerString)
{
if (item == player[index])
aCount++;
else if (player.Contains(item))
bCount++;
index++;
}
return string.Format("{0}A{1}B", aCount, bCount);
}
不要激动,答案给你了,我没有办法调试,你自己试一下,我不能保证绝对正确.哪句不理解再问.
虽然输入的是数字,但是不一定非要把东西当作数字来处理,
比如随机4个不重复的数字,
Random 方法有个参数可以指定取值范围,
我们先把0~9 push到一个集合中.比如List<int>
然后我们第一次随机0~9之间的数字,把这个数字当作索引在List中取值然后从List中删除,第二次我们随机0~8之间的数字,以此类推.
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleAppGuessNum
{
//一个生成四位随机数字的类
class GetRandomNum
{
private List<Int32> list_num = new List<int>();
//构造函数,将私有成员list_num赋值,里面存放的所有四位自然正整数
public GetRandomNum()
{
for (int i = 1000; i < 10000; i++)
{
list_num.Add(i);
}
}
//生成这个随机数
public int RandomNum()
{
Random rd = new Random();
int index = rd.Next(8999);
while(HasSameNum(GetEveryNum(list_num[index])))
{
index = rd.Next(8999);
}
return list_num[index];
}
//判断这个四位数里面有没有重复的数字
public static bool HasSameNum(List<Int32> TestList)
{
for (int i = 0; i < 4; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (TestList[i] == TestList[j])
{
return true;
}
}
}
return false;
}
//取得四位数每一位数字
public static List<Int32> GetEveryNum(int TestNum)
{
List<Int32> list_EveryNum = new List<int>(4);
list_EveryNum.Add(TestNum / 1000);
list_EveryNum.Add((TestNum % 1000) / 100);
list_EveryNum.Add((TestNum % 100) / 10);
list_EveryNum.Add(TestNum % 10);
return list_EveryNum;
}
}
//一个猜数字的类
class GuessNum
{
//构造函数
public GuessNum()
{
}
//判断用户输入的数字是不是合理的四位数
public int IfInvalNum(string str_InputNum)
{
if (str_InputNum.Length != 4)
{
return 0;
}
else
{
int value = 0;
if (Int32.TryParse(str_InputNum, out value))
{
if (value >= 1000 && value < 10000)
{
if (!GetRandomNum.HasSameNum(GetRandomNum.GetEveryNum(value)))
{
return value;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
//判断有几个A和几个B
public bool CheckNum(List<Int32> list_rd,List<Int32> list_Gs)
{
List<Int32> list_AB = new List<int>();
int _NumOfA = 0;
int _NumOfB = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (list_Gs[i] == list_rd[j])
{
if (i == j)
{
_NumOfA++;
}
else
{
_NumOfB++;
}
}
}
}
Console.WriteLine("{0}A{1}B",_NumOfA,_NumOfB);
if (_NumOfA == 4)
{
Console.WriteLine("恭喜你,猜对了!");
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
bool IfAgain = false;
do
{
GetRandomNum getrandomnum = new GetRandomNum();
int TheNum = getrandomnum.RandomNum();
TheNum = 1235;
GuessNum guessnum = new GuessNum();
bool IfContinus = true;
do
{
Console.WriteLine("请输入你猜测的数字:");
int TheGuessNum = guessnum.IfInvalNum(Console.ReadLine());
if (TheGuessNum != 0)
{
//List<Int32> list_TheNum = GetRandomNum.GetEveryNum(TheNum);
IfContinus = false;
if (guessnum.CheckNum(GetRandomNum.GetEveryNum(TheNum),
GetRandomNum.GetEveryNum(TheGuessNum)))
{
Console.WriteLine("继续请输入g,离开请输入其他任意键:");
if (Console.ReadLine() == "g")
{
IfAgain = true;
}
else
{
IfAgain = false;
}
}
}
else
{
Console.WriteLine("你输入的数字不符合要求,请重新输入:");
}
}
while (IfContinus);
}
while (IfAgain);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleAppGuessNum
{
//一个生成四位随机数字的类
class GetRandomNum
{
private List<Int32> list_num = new List<int>();
//构造函数,将私有成员list_num赋值,里面存放的所有四位自然正整数
public GetRandomNum()
{
for (int i = 1000; i < 10000; i++)
{
list_num.Add(i);
}
}
//生成这个随机数
public int RandomNum()
{
Random rd = new Random();
int index = rd.Next(8999);
while(HasSameNum(GetEveryNum(list_num[index])))
{
index = rd.Next(8999);
}
return list_num[index];
}
//判断这个四位数里面有没有重复的数字
public static bool HasSameNum(List<Int32> TestList)
{
for (int i = 0; i < 4; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (TestList[i] == TestList[j])
{
return true;
}
}
}
return false;
}
//取得四位数每一位数字
public static List<Int32> GetEveryNum(int TestNum)
{
List<Int32> list_EveryNum = new List<int>(4);
list_EveryNum.Add(TestNum / 1000);
list_EveryNum.Add((TestNum % 1000) / 100);
list_EveryNum.Add((TestNum % 100) / 10);
list_EveryNum.Add(TestNum % 10);
return list_EveryNum;
}
}
//一个猜数字的类
class GuessNum
{
//构造函数
public GuessNum()
{
}
//判断用户输入的数字是不是合理的四位数
public int IfInvalNum(string str_InputNum)
{
if (str_InputNum.Length != 4)
{
return 0;
}
else
{
int value = 0;
if (Int32.TryParse(str_InputNum, out value))
{
if (value >= 1000 && value < 10000)
{
if (!GetRandomNum.HasSameNum(GetRandomNum.GetEveryNum(value)))
{
return value;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
//判断有几个A和几个B
public bool CheckNum(List<Int32> list_rd,List<Int32> list_Gs)
{
List<Int32> list_AB = new List<int>();
int _NumOfA = 0;
int _NumOfB = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (list_Gs[i] == list_rd[j])
{
if (i == j)
{
_NumOfA++;
}
else
{
_NumOfB++;
}
}
}
}
Console.WriteLine("{0}A{1}B",_NumOfA,_NumOfB);
if (_NumOfA == 4)
{
Console.WriteLine("恭喜你,猜对了!");
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
bool IfAgain = false;
do
{
GetRandomNum getrandomnum = new GetRandomNum();
int TheNum = getrandomnum.RandomNum();
TheNum = 1235;
GuessNum guessnum = new GuessNum();
bool IfContinus = true;
do
{
Console.WriteLine("请输入你猜测的数字:");
int TheGuessNum = guessnum.IfInvalNum(Console.ReadLine());
if (TheGuessNum != 0)
{
//List<Int32> list_TheNum = GetRandomNum.GetEveryNum(TheNum);
IfContinus = false;
if (guessnum.CheckNum(GetRandomNum.GetEveryNum(TheNum),
GetRandomNum.GetEveryNum(TheGuessNum)))
{
Console.WriteLine("继续请输入g,离开请输入其他任意键:");
if (Console.ReadLine() == "g")
{
IfAgain = true;
}
else
{
IfAgain = false;
}
}
}
else
{
Console.WriteLine("你输入的数字不符合要求,请重新输入:");
}
}
while (IfContinus);
}
while (IfAgain);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Random r = new Random();
int a=Convert.Toint32( r.Next())*10;
a是 0-9的数了
做4个就可以了
剩下的 只拿if就能解决
主要是随机的地方
你现在可以去试着解决了
我说的是你写出来的!!????
你写随机函数了????
我已经写出了改写的 新手写他已经没有任何问题了...
你要捡现成的 等人做好的 那你等吧..
奥?
麻烦??不明白...这题思路很简单 代码是多了点 但是你要是有信心做下去 会学到很多东西啊
int a=Convert.Toint32( r.Next())*10;
a是 0-9的数了
做4个就可以了
剩下的 只拿if就能解决
主要是随机的地方
你现在可以去试着解决了
我说的是你写出来的!!????
你写随机函数了????
我已经写出了改写的 新手写他已经没有任何问题了...
你要捡现成的 等人做好的 那你等吧..
奥?
麻烦??不明白...这题思路很简单 代码是多了点 但是你要是有信心做下去 会学到很多东西啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace NumberPuzzle
{
class Program
{
/// <summary>
/// Num Puzzle
/// ^^**
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string numPazzle = string.Empty;
string numLength=string.Empty;
int count = 0;
int countMax = 0;
Console.WriteLine("How long do you want?");
while (true)
{
numLength = Console.ReadLine();
if (IsNum(numLength))
{
break;
}
else
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
}
countMax = Convert.ToInt32(numLength);
while (count < countMax)
{
string strA = GetNum();
if (numPazzle.IndexOf(strA) != -1)
{
continue;
}
numPazzle += strA;
count++;
}
while (true)
{
string input = string.Empty;
string results = string.Empty;
Console.WriteLine("Input what you guess:");
input = Console.ReadLine();
if (input.Length != countMax)
{
Console.WriteLine("The length of input is error");
continue;
}
else if (IsDup(input))
{
Console.WriteLine("Input is a dup num");
continue;
}
results = CompareNum(input, numPazzle);
if (results.Split('-')[0].Equals(numPazzle.Length.ToString()))
break;
Console.WriteLine("Results: A-{0} B-{1}",results.Split('-')[0],results.Split('-')[1]);
}
Console.WriteLine("Win! The num is {0}", numPazzle);
Console.ReadKey();
}
public static string GetNum()
{
Random sSeed = new Random();
Random seed = new Random(sSeed.Next());
return seed.Next(10).ToString();
}
public static string CompareNum(string actualStr, string expectedStr)
{
int a = 0;
int b = 0;
string results=string.Empty;
for (int i = 0; i < actualStr.Length; i++)
{
if (expectedStr.IndexOf(actualStr[i]) != -1)
{
b++;
}
if (expectedStr[i].Equals(actualStr[i]))
{
a++;
b--;
}
}
results=a.ToString()+"-"+b.ToString();
return results;
}
public static bool IsDup(string input)
{
bool result = false;
foreach (char aStr in input)
{
if (input.IndexOf(aStr) != input.LastIndexOf(aStr))
{
result = true;
break;
}
}
return result;
}
public static bool IsNum(string numInput)
{
bool result = false;
Regex reg = new Regex(@"^-?\d+$");
result = reg.IsMatch(numInput);
return result;
}
}
}
能想到验证的东西都想到了 位数你选择 由于是不重复的数所以位数限定在10位之内包括10位
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace NumberPuzzle
{
class Program
{
/// <summary>
/// Num Puzzle
/// ^^**
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string numPazzle = string.Empty;
string numLength=string.Empty;
int count = 0;
int countMax = 0;
Console.WriteLine("How long do you want?");
while (true)
{
numLength = Console.ReadLine();
if (IsNum(numLength))
{
break;
}
else
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}
}
countMax = Convert.ToInt32(numLength);
while (count < countMax)
{
string strA = GetNum();
if (numPazzle.IndexOf(strA) != -1)
{
continue;
}
numPazzle += strA;
count++;
}
while (true)
{
string input = string.Empty;
string results = string.Empty;
Console.WriteLine("Input what you guess:");
input = Console.ReadLine();
if (input.Length != countMax)
{
Console.WriteLine("The length of input is error");
continue;
}
else if (IsDup(input))
{
Console.WriteLine("Input is a dup num");
continue;
}
results = CompareNum(input, numPazzle);
if (results.Split('-')[0].Equals(numPazzle.Length.ToString()))
break;
Console.WriteLine("Results: A-{0} B-{1}",results.Split('-')[0],results.Split('-')[1]);
}
Console.WriteLine("Win! The num is {0}", numPazzle);
Console.ReadKey();
}
public static string GetNum()
{
Random sSeed = new Random();
Random seed = new Random(sSeed.Next());
return seed.Next(10).ToString();
}
public static string CompareNum(string actualStr, string expectedStr)
{
int a = 0;
int b = 0;
string results=string.Empty;
for (int i = 0; i < actualStr.Length; i++)
{
if (expectedStr.IndexOf(actualStr[i]) != -1)
{
b++;
}
if (expectedStr[i].Equals(actualStr[i]))
{
a++;
b--;
}
}
results=a.ToString()+"-"+b.ToString();
return results;
}
public static bool IsDup(string input)
{
bool result = false;
foreach (char aStr in input)
{
if (input.IndexOf(aStr) != input.LastIndexOf(aStr))
{
result = true;
break;
}
}
return result;
}
public static bool IsNum(string numInput)
{
bool result = false;
Regex reg = new Regex(@"^-?\d+$");
result = reg.IsMatch(numInput);
return result;
}
}
}
能想到验证的东西都想到了 位数你选择 由于是不重复的数所以位数限定在10位之内包括10位
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
StringBuilder R = new StringBuilder();//用来保存随机数
//-----------------生成随机数---------------------
Hashtable hashtable = new Hashtable();
Random rm = new Random();
int RmNum = 4;
for (int i = 0; hashtable.Count < RmNum; i++)
{
int nValue = rm.Next(9);
if (!hashtable.ContainsValue(nValue) && nValue != 0)
{
hashtable.Add(nValue, nValue);
R.Append(nValue.ToString());
}
}
//-----------------生成随机数---------------------
//*注:请将生成随机数保存为一个方法后获得返回值
char[] c = R.ToString().ToCharArray();//将4个随机数分开存到数组中
char[] input = textBox1.Text.ToCharArray();//获得输入的随机,同时分开存到数组中
int A = 0;//记录位置和数字一样的情况
int B = 0;//记录位置不同数字一样的情况
//---------------开始判断-------------------------
for (int i = 0; i < 4; i++)
{
if (c[i] == input[i])//判断记录位置和数字一样的情况
{
A++;
}
for (int j = 0; j < 4; j++)//判断记录位置不同数字一样的情况
{
if (c[i] == input[j] && j != i)
{
B++;
}
}
}
//-------------判断结束,弹出随即数字和结果-----------
if(A!=4)
MessageBox.Show(R + "\r\n" + A + "A " + B + "B","猜随即数字结果!",MessageBoxButtons.OK,MessageBoxIcon.Information);
else
MessageBox.Show("恭喜你!猜对了!", "猜随即数字结果!", MessageBoxButtons.OK, MessageBoxIcon.Information);
//-----------------生成随机数---------------------
Hashtable hashtable = new Hashtable();
Random rm = new Random();
int RmNum = 4;
for (int i = 0; hashtable.Count < RmNum; i++)
{
int nValue = rm.Next(9);
if (!hashtable.ContainsValue(nValue) && nValue != 0)
{
hashtable.Add(nValue, nValue);
R.Append(nValue.ToString());
}
}
//-----------------生成随机数---------------------
//*注:请将生成随机数保存为一个方法后获得返回值
char[] c = R.ToString().ToCharArray();//将4个随机数分开存到数组中
char[] input = textBox1.Text.ToCharArray();//获得输入的随机,同时分开存到数组中
int A = 0;//记录位置和数字一样的情况
int B = 0;//记录位置不同数字一样的情况
//---------------开始判断-------------------------
for (int i = 0; i < 4; i++)
{
if (c[i] == input[i])//判断记录位置和数字一样的情况
{
A++;
}
for (int j = 0; j < 4; j++)//判断记录位置不同数字一样的情况
{
if (c[i] == input[j] && j != i)
{
B++;
}
}
}
//-------------判断结束,弹出随即数字和结果-----------
if(A!=4)
MessageBox.Show(R + "\r\n" + A + "A " + B + "B","猜随即数字结果!",MessageBoxButtons.OK,MessageBoxIcon.Information);
else
MessageBox.Show("恭喜你!猜对了!", "猜随即数字结果!", MessageBoxButtons.OK, MessageBoxIcon.Information);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询