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

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 21:46:32
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.
这个是用VS创建网站时自动加上去的,如果你的代码中没有引用的话可以删去
这个是我修改后的代码,里面有很详细的注释,呜呜呜呜,忙了一个上午,可惜没有分啊,呜呜呜呜呜.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace NumberPuzzle
{
class Program /*这个程序实际上就是一个猜数字的游戏,先输入一个数字,然后程序生成一个长度为它的未知数,然后你来猜那个未知数*/
{
///
/// Num Puzzle
/// ^^**
///
///
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?010"); //如果n>10,就提示重新输入
continue;
}
break;
}
else
{
Console.WriteLine("Re-inpt, input is not a num:"); //否则如果输入的字符串不能转换成一个数值,重新输入
continue;
}
}

while (count < countMax) //循环,这个循环的作用就是生成一个与你输入的那个有效的字符串的长度相同的数字,也就是那个不知道的要猜测的数字
{
string strA = GetNum(); //获取一个字符串(实际是一个不超过10的随机数字)
if (numPazzle.IndexOf(strA) != -1) //String.IndexOf (String) 报告指定的 String 在此实例中的第一个匹配项的索引.[MSDN]
{
continue; //如果numPzzle中存在strA,那么就继续进行循环,不执行下面的操作,这也就是说生成的那个未知数中不含相同的数字
}
numPazzle += strA; //把strA追加到字符串numPazzle后面,实际上就是形成另一个数字
count++;
}
while (true)
{
string input = string.Empty;
string results = string.Empty;
Console.WriteLine("Input what you guess:"); //输入你猜测的字符串
input = Console.ReadLine(); //获取你输入的字符串,传给input

if (!IsNum(input)) //如果你输入的字符串不可以转换成一个数字就重新输入
{
Console.WriteLine("Re-inpt, input is not a num:");
continue;
}

if (input.Length != countMax) //如果你输入的字符串的长度不等于countMax--->前面countMax已经被赋值了,countMax = Convert.ToInt32(numLength);
{
Console.WriteLine("The length of input is error"); //输入的字符串的长度错误,继续重新输入
continue;
}

if (IsDup(input)) //如果输入的字符串是一个“重复字符串”【看后面的解释】,就先进入下一步
{
Console.WriteLine("Input is a dup num");
continue;
}

results = CompareNum(input, numPazzle); //比较两个字符串,看他们的“相似度”【看后面的解释】
if (results.Split('-')[0].Equals(numPazzle.Length.ToString())) //如果比较的两个字符串相同字符的位置相同的个数(a)等于期望的字符串的长度[实际上就是两个字符串相等]
break; //String.Split (Char[]) 返回包含此实例中的子字符串(由指定 Char 数组的元素分隔)的 String 数组.[MSDN] 【注: 这里的0和1应该放在[]里面】
/*results.Split('-')得到的是一个字符数组,results.Split('-')[0]就是后面的a,results.Split('-')[1]就是b*/
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() //该方法获取一个字符串(实际是一个随机数字,不超过10)
{
Random sSeed = new Random(); //随机生成数字
Random seed = new Random(sSeed.Next()); //random的重载方法Next(Int32) 返回一个小于所指定最大值的非负随机数.
return seed.Next(10).ToString();
}
public static string CompareNum(string actualStr, string expectedStr) //比较两个字符串,看他们的“相似度”
{ /*这个方法的意思就是说,用actualStr【实际的猜测的字符串】去对比expectedStr【期望的目标字符串】*/
int a = 0; /*a表示的是存在而且位置正确的字符个数,也就是猜对了具体的几个数字*/
int b = 0; /*b表示的是存在但是位置不正确的字符个数,也就是猜对了数字,但是数字的位置不对*/
string results = string.Empty;
for (int i = 0; i < actualStr.Length; i++)
{
if (expectedStr.IndexOf(actualStr[i]) != -1) //注意这里actualStr后面有[]
{
b++; //如果expectedStr字符串中含有actualStr的第i个字符,那么b+1
}
if (expectedStr[i].Equals(actualStr[i])) //如果expectedStr中的第i个字符和actualStr中的第i个字符相等的话,a+1,b-1
{
a++;
b--;
}
}
results = a.ToString() + "-" + b.ToString(); //返回 a-b
return results;
}
public static bool IsDup(string input) //判断输入的字符串是否是“重复字符串”,实际就是判断你输入的数值中是否含有重复的数字
{
bool result = false;
foreach (char aStr in input) //遍历输入的字符串input的每个字符
{
if (input.IndexOf(aStr) != input.LastIndexOf(aStr)) //String.LastIndexOf (Char) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置.[MSDN]
{
result = true; //如果输入的字符串中的每个字符第一次出现和最后出现不是在相同的位置,那就返回true,并退出循环,因为这个肯定猜错了!
break; //也就是说输入的字符串中出现的每个字符都不止一次出现----->“重复字符串”的意思【我写的,为了理解】
}
}
return result;
}
public static bool IsNum(string numInput) //判断输入的字符串是否可以匹配一个非负整数
{
bool result = false;
Regex reg = new Regex(@"^-?\d+$"); //非负整数,正则表达式
result = reg.IsMatch(numInput);
return result; //如果输入的字符串是非负整数的话,就返回true
} //方法
} //类Program
} //命名空间NumberPuzzle

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System. using System; using System.Collections; using System.Configuration; using System.Data; using System.using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons using System.Linq 关于c# using systemusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms; using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; usi 有无语法错误,帮我改改谢谢using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using Sys c#高手看看什么问题 using System; using System.Collections.Generic; using System.ComponentModel; usiusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System using 摄氏温度和华氏温度之间的转换.using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _11_1{public partial 求0-100所有的质数用c#编写using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WindowsApplication2{ public partial class For using System; using System.Collections.Generic; using System.Windows.Forms; namespace gxz { static using System;using System.Collections.Generic;using System.Windows.Forms;namespace gxz{static class Program{/// /// 应用程序的主入口点./// [ST c_sharp中using system.linq表示什么? using System.Data.SqlClient;的真实含义? using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; u提示“system”中不存在类型或命名空间名称‘Ling’(缺少程序集引用吗?) 求大神帮下忙,这个错哪里了呀,怎么修改using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace hanshu2{ class Program { static void MaxValue(int[] intArray,ou The method was developed using a representative system.句中using是什么成分? 帮我注释一下每行代码,就是解释下每行的意思,谢谢了using System;using System.Globalization;using System.Text.RegularExpressions;using System.Windows.Forms; namespace Text{ public partial class Form1 : Form { private s