C#中如何查询控制台输入的东西?

题目是这样的:
1.请从控制台输入一些字符,然后输入相匹配的字符,如果有一样的请输入有几个一样的。
例子:控制台输入:kdsdsba32155
输出的内容是:
您总共输入了12个字符
输出一个k
输入了2个d
输入了两个s
输入了一个b
输入了一个a
输入了一个3
输入了一个2
输入了一个1
输入了两个5
请各位帮忙。

源代码
主类:

using System;

namespace NumGuess
{
/// <summary>
/// Guess 的摘要说明。
/// </summary>
class Guess
{

ColorInput colorText = new ColorInput(); //控制台彩色输出类
private string choose = null; //对于 菜单的 选择
private int[] random = new int[4]; //随机产生的 一个 4位数
private int[] guess = new int[4]; //存储 用户 猜测的 4位数
private bool again = false; //是否是 继续游戏 而不是开始新游戏

//构造方法
public Guess()
{
}

//显示游戏开始的一些信息
void Game()
{
colorText.SetTextColor((int) ColorInput.Foreground.Blue); //设置控制台输出字符的颜色
Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("\t游戏说明:游戏开始,您要猜随机产生的一个四位数,"
+ "数字的");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("位置");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("和");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("大小");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("都要猜对才算赢哦,有点像彩票哦,不过您每次只有");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("10");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("次机会哦~~!!!\n");
colorText.SetTextColor((int) ColorInput.Foreground.Purple);
Console.WriteLine("\t\t\t\t\t\t\t----By H_Q");

colorText.SetTextColor((int) ColorInput.Foreground.White);
Console.WriteLine("\t\t\t\t菜单(请选择)");
Console.WriteLine("\t\t\t\t 1.开始");
Console.WriteLine("\t\t\t\t 0.退出");
Init();
}// Game

void Init()
{
choose = Console.ReadLine();
//判断 用户的 选择
while (choose != "0")
{
if (choose.Equals("1"))
{
InitGame();
break;
}
//当 10次 中没有猜对 判断是否要继续游戏
if (choose.Equals("2") && again)
{
Input();
again = false;
}
else
{
Console.Write("请按游戏菜单,输入 1 或 0: ");
choose = Console.ReadLine();
continue;
}
} // while
} // Init()

//游戏开始
void InitGame()
{
Random r = new Random();
int ran = r.Next(1000, 9999); //随机产生一个 4位数 下限1000 上限9999
//将该 4位数 存储到数组中
for (int i =3; i >= 0; i --)
{
random[i] = ran%10;
ran /= 10;
}
Input();
} // InitGame()

//接受用户的 输入的 数字
void Input()
{
int count = 0; //用于记录 用户猜测的次数 10次 没有猜对为输
bool isFormat = false;
//若输入的不正确 循环要求用户输入
do
{
Console.Write("请您输入您猜测的4位数字: ");
string inputNum = Console.ReadLine();
//检查用户 输入的合法性
if (inputNum.Length == 4)
{
int j = 0;
foreach (char ch in inputNum)
{
if (Char.IsNumber(ch))
{
j ++;
}
}
if (j == 4)
{
//将正确的输入 存入 猜测数组中
for (int i = 0; i < 4; i ++)
{
guess[i] = int.Parse(inputNum.Substring(i, 1));
}
count ++;
isFormat = Compare(count);
}
else
{
isFormat = false;
}
}
else
{
isFormat = false;
}
}while (count < 10 && isFormat == false);
}//Input()

//比较 用户猜测的 数字 与 随机产生的数字 的位置 及 每位数的大小
bool Compare(int count)
{
string sameG = null; //用于标识 用户猜测的数字中 已经 判等过的
string sameR = null; //用于标识 随机产生的数字中 已经 被判等过的
int s = 0; //用于标识 用户猜对大小的数字的个数
int p = 0; //用于标识 用户猜对位置的数字的个数

//顺序判等 用来 排除一次就猜对的情况
for (int i = 0; i < 4; i ++)
{
if (guess[i] == random[i])
{
p ++;
s ++;
sameG += i;
sameR += i;
}
}

//一次就猜对的情况
if (p == 4 && s == 4)
{
Win();
return true;
}
else
{
for (int g = 0; g < 4; g ++)
{
for (int r = 0; r < 4; r ++)
{
//如果顺序比较中没有一个猜对那么 sameG sameR都为null
//如果不为空 那么通过 IndexOf 方法 将 已经判等过的 排除了 不须再次比较
//注: IndexOf(string) 此方法 从 string 中查找相同字符 若没找到 返回-1
if (sameG == null || sameR == null ||
(sameR.IndexOf(r.ToString()) == -1 && sameG.IndexOf(g.ToString()) == -1))
{
if (guess[g] == random[r])
{
s ++;
sameG += g;
sameR += r;
break;
}
}
}
}
//在10次内猜对的情况
if (s == 4 && p == 4)
{
Win();
return true;
}
else
{
if (count >= 10)
{
Lost();
return true;
}
else
{
Console.WriteLine("猜对数字的个数为:{0}, 猜对位置的个数为:{1}, 您还有{2}次机会\n", s, p,10 - count);
return false;
}
}
}
} //Compare()

//赢咯~
void Win()
{
colorText.SetTextColor((int)ColorInput.Foreground.Green);
Console.WriteLine("\t\t\t\t您赢了\n");
colorText.ResetColor(); //还原控制台 默认的 输出颜色
Console.WriteLine("\t\t\t\t 菜单");
Console.WriteLine("\t\t\t\t1.新游戏");
Console.WriteLine("\t\t\t\t0.退出");
colorText.SetTextColor((int)ColorInput.Foreground.White);
Init();
}

//再接再厉
void Lost()
{
colorText.SetTextColor((int)ColorInput.Foreground.Red);
Console.WriteLine("\t\t\t\t您输了");
colorText.ResetColor();
Console.WriteLine("\t\t\t\t 菜单");
Console.WriteLine("\t\t\t\t1.新游戏");
Console.WriteLine("\t\t\t\t2.继续");
Console.WriteLine("\t\t\t\t0.退出");
again = true; //标识 是继续 这样不重新产生随机的4位数
colorText.SetTextColor((int)ColorInput.Foreground.White);
Init();
}

public static void Main(string[] args)
{
Guess game = new Guess();
game.Game();
}

} //class
} // namespace

控制 控制台 彩色输出类:

using System;
using System.Runtime.InteropServices;

namespace NumGuess
{
//用于改变控制台输出颜色
public class ColorInput
{
private int hConsoleHandle;

private const int STD_OUTPUT_HANDLE = -11;

[DllImport("kernel32.dll")]
private static extern int GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]
private static extern int SetConsoleTextAttribute(int hConsoleOutput,
int wAttributes);

// 构造方法
public ColorInput()
{
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}

//自己试出来的高亮颜色
public enum Foreground
{
Green = 10,
Blue,
Red,
Purple,
Golder,
White
}

public void SetTextColor(int color)
{
SetConsoleTextAttribute(hConsoleHandle, color);
}

public void ResetColor()
{
SetConsoleTextAttribute(hConsoleHandle, 7);
}
}//class
}//namespace

.Net 2003 中 C# 复杂控制台清屏 类

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace NumGuess
{
class StdHandleEnum
{
public const int STD_INPUT_HANDLE = -10;
public const int STD_OUTPUT_HANDLE = -11;
public const int STD_ERROR_HANDLE = -12;
};

// This sructure contains a screen coordinate.
class cls
{
internal struct COORD
{
public short X;
public short Y;
}

// 屏幕缓冲区信息
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD Size;
public COORD p1;
public short a1;
public short w1;
public short w2;
public short w3;
public short w4;
public COORD m1;
}

/*
* Kernel32.dll 中 4个函数
*/

//返回 handle 给 任何标准输入输出
[DllImport("kernel32.dll")]
public static extern int GetStdHandle(int nStdHandle);

//返回 控制台 缓冲区 信息
[DllImport("kernel32.dll")]
public static extern bool GetConsoleScreenBufferInfo(int hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

//放置光标
[DllImport("kernel32.dll")]
public static extern bool SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);

// 不知道该如何翻译
// The FillConsoleOutputCharacter() allows us to place
// any character on the console screen. Using a space
// clears the display area.
[DllImport("kernel32.dll",SetLastError=true,CharSet=CharSet.Auto)]
public static extern bool FillConsoleOutputCharacter(int hConsoleOutput, short cCharacter, int nLength, COORD WriteCoord, out int lpNumberOfCharsWritten);

[STAThread]
public static void Clear()
{
// Needed ask Windows about the console screen
// buffer settings.
CONSOLE_SCREEN_BUFFER_INFO CSBI;
// Handle to the output device.
int hOut;
// Number of characters written to the screen.
int CharOut;
// Home cursor position.
COORD Home;

// Clear the screen.
// Begin by getting a handle to the console screen.
hOut = GetStdHandle(StdHandleEnum.STD_OUTPUT_HANDLE);

// Get the required console screen buffer information.
GetConsoleScreenBufferInfo(hOut, out CSBI );

// Set the home position for the cursor.
Home.X = 0;
Home.Y = 0;

// Fill the console with spaces.
FillConsoleOutputCharacter(hOut,
(short) ' ',
CSBI.Size.X * CSBI.Size.Y,
Home,
out CharOut);

// Place the cursor in the upper left corner.
SetConsoleCursorPosition(hOut, Home);

Console.WriteLine("\t\t\t\t游戏开始咯~\n");
} // cls
}// class
}//namespace
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-27
public static void Main()
{
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
PutChar(str);
Console.ReadLine();
}

public static void PutChar(string str)
{
if (str == string.Empty) return;
char ch = str.Substring(0).ToCharArray()[0];
int num = str.Split(ch).Length - 1;
Console.WriteLine("char: {0}", CharCount(ch, num));
PutChar(str.Replace(ch.ToString(), ""));
}

public static string CharCount(char ch, int num)
{
int i=0;
string str = string.Empty;
while (i < num)
{
str += ch;
i++;
}
return str;
}本回答被提问者采纳
第2个回答  2009-05-27
哎,在写程序的时候1楼被占了。
不过我的程序结果应该和你想要的一样。

class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入");
string strArr = Console.ReadLine();
int count = 0;
string strCheck = string.Empty;
Console.WriteLine("您总共输入了" + strArr.Length + "个字符");

for (int i = 0; i < strArr.Length; ++i)
{
if (strCheck.IndexOf(strArr[i]) == -1)
{
strCheck += strArr[i];
for (int j = 0; j < strArr.Length; ++j)
{
if (strArr[i] == strArr[j])
{
++count;
}
}
}
if (count != 0)
{
Console.WriteLine("输出了" + count + "个" + strArr[i]);
}
count = 0;
}
}
}
第3个回答  2009-05-27
回答者: 3306263wb - 举人 四级 2009-5-27 10:22

正解!