求助,求帮忙
这是一个猜字符的小游戏,但是我运行的时候总显示 非静态的字段、方法或属性“ConsoleApplication6.Program.StartGuess()”要求对象引用 我用的是Microsoft Visual Studio 2010 求怎么改才能用using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
StartGuess();
}
private void StartGuess()
{
Func<char, string> hint = new Func<char, string>(chr => string.Concat("当前是一个", 'A' < chr && chr < 'Z' ? "大写字母" : ('a' < chr && chr < 'z' ? "小写字母" : "数字")));
Func<char, Action, bool> guess = new Func<char, Action, bool>((chr, action) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(hint(chr));
Console.ForegroundColor = ConsoleColor.Black;
int innerguesscount = 0;
bool stop = false;
while (!stop)
{
Console.Write("当前已猜{0}次,请输入你猜的数:", innerguesscount);
try
{
string x = Console.ReadLine();
if (x.ToLower() == "quit") { action(); return true; };
char c = '-';
if (!char.TryParse(x, out c) || '0' > c || c > 'z' || Char.IsWhiteSpace(c)) throw new Exception();
Console.Write(c);
Console.ForegroundColor = ConsoleColor.Red;
if (c > chr) Console.WriteLine("\r\n提示:输入的字符太大");
else if (c < chr) Console.WriteLine("\r\n输入的字符太小");
else
{
Console.WriteLine("\r\n恭喜你,猜对了");
return true;
}
Console.ForegroundColor = ConsoleColor.Black;
innerguesscount++;
}
catch (Exception e)
{
Console.WriteLine("\r\n警告:输入的不是字母,也不是数字,重新输入吧");
}
}
return false;
});
var array = Enumerable.Concat(Enumerable.Concat(
RandomChoice(Enumerable.Range((byte)'A', 26).Select(n => (char)n).ToArray(), 13),
RandomChoice(Enumerable.Range((byte)'a', 26).Select(n => (char)n).ToArray(), 15)),
RandomChoice(Enumerable.Range((byte)'0', 10).Select(n => (char)n).ToArray(), 7)).OrderBy(c => c).ToArray();
Console.WriteLine("猜字符(输入quit退出游戏)\r\n");
int index = 0;
while (index < array.Length)
{
Console.WriteLine();
guess(array[index], () => { index = int.MaxValue - 1; Console.WriteLine("\r\n游戏结束"); });
index++;
}
}
private List<T> RandomChoice<T>(T[] source/*Array类*/, int needCount)
{
List<T> result = new List<T>();//集合类
if (source == null || source.Length == 0) return result;
int length = source.Length;
if (needCount == 0) needCount = length;
int[] indexs = new int[length];
for (int i = 0; i < length; i++) indexs[i] = i;
const int MAXCOUNT = 1000;
Random rand = new Random(DateTime.Now.Millisecond);
int count = 0;
while (MAXCOUNT > count)
{
int first = rand.Next(0, length);
int second = rand.Next(0, length);
if (first == second) continue;
int tmp = indexs[first];
indexs[first] = indexs[second];
indexs[second] = tmp;
count++;
}
for (int i = 0; i < needCount; i++) result.Add(source[indexs[i]]);
return result;
}
}
}
--------------------编程问答-------------------- private static ... --------------------编程问答-------------------- 加在哪啊 --------------------编程问答-------------------- private static void StartGuess()
private static List<T> RandomChoice<T> --------------------编程问答--------------------
using System;--------------------编程问答-------------------- private static void StartGuess()
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
namespace ConsoleApplication6
{
public static class Program {
static void Main(string[] args)
{
StartGuess();
}
private static void StartGuess() {
Func<char, string> hint = new Func<char, string>(chr => string.Concat("当前是一个", 'A' < chr && chr < 'Z' ? "大写字母" : ('a' < chr && chr < 'z' ? "小写字母" : "数字")));
Func<char, Action, bool> guess = new Func<char, Action, bool>((chr, action) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(hint(chr));
Console.ForegroundColor = ConsoleColor.Black;
int innerguesscount = 0;
bool stop = false;
while (!stop)
{
Console.Write("当前已猜{0}次,请输入你猜的数:", innerguesscount);
try
{
string x = Console.ReadLine();
if (x.ToLower() == "quit") { action(); return true; };
char c = '-';
if (!char.TryParse(x, out c) || '0' > c || c > 'z' || Char.IsWhiteSpace(c)) throw new Exception();
Console.Write(c);
Console.ForegroundColor = ConsoleColor.Red;
if (c > chr) Console.WriteLine("\r\n提示:输入的字符太大");
else if (c < chr) Console.WriteLine("\r\n输入的字符太小");
else
{
Console.WriteLine("\r\n恭喜你,猜对了");
return true;
}
Console.ForegroundColor = ConsoleColor.Black;
innerguesscount++;
}
catch (Exception e)
{
Console.WriteLine("\r\n警告:输入的不是字母,也不是数字,重新输入吧");
}
}
return false;
});
var array = Enumerable.Concat(Enumerable.Concat(
RandomChoice(Enumerable.Range((byte)'A', 26).Select(n => (char)n).ToArray(), 13),
RandomChoice(Enumerable.Range((byte)'a', 26).Select(n => (char)n).ToArray(), 15)),
RandomChoice(Enumerable.Range((byte)'0', 10).Select(n => (char)n).ToArray(), 7)).OrderBy(c => c).ToArray();
Console.WriteLine("猜字符(输入quit退出游戏)\r\n");
int index = 0;
while (index < array.Length)
{
Console.WriteLine();
guess(array[index], () => { index = int.MaxValue - 1; Console.WriteLine("\r\n游戏结束"); });
index++;
}
}
private static List<T> RandomChoice<T>(T[] source/*Array类*/, int needCount)
{
List<T> result = new List<T>();//集合类
if (source == null || source.Length == 0) return result;
int length = source.Length;
if (needCount == 0) needCount = length;
int[] indexs = new int[length];
for (int i = 0; i < length; i++) indexs[i] = i;
const int MAXCOUNT = 1000;
Random rand = new Random(DateTime.Now.Millisecond);
int count = 0;
while (MAXCOUNT > count)
{
int first = rand.Next(0, length);
int second = rand.Next(0, length);
if (first == second) continue;
int tmp = indexs[first];
indexs[first] = indexs[second];
indexs[second] = tmp;
count++;
}
for (int i = 0; i < needCount; i++) result.Add(source[indexs[i]]);
return result;
}
}
}
private static List<T> RandomChoice<T>(T[] source/*Array类*/, int needCount) --------------------编程问答-------------------- kao,
csdn啊,加样式都没办法正常显示 --------------------编程问答-------------------- private也好,public也好,只要加上static就行,或者你new ConsoleApplication6.Program().StartGuess() --------------------编程问答-------------------- 输入进去之后是可以运行了,但是运行之后出来的Dos界面输入的东西不显示 就是你输入的时候光标会往后动,但是不显示东西 --------------------编程问答-------------------- --------------------编程问答-------------------- 非静态类的调用要实例化,静态类的调用不需要实例化,你的类名前没有加public,加了public后还要实例化才能调用.
如果使用表态类调用,直接使用表态类名加表态方法,上面我改为表态类和表态方法,可以直接调用 --------------------编程问答-------------------- 不能显示输入输出信息是什么情况啊
--------------------编程问答--------------------
丢掉这份代码然后自己写一个 --------------------编程问答--------------------
我知道是什么情况了,你把输入字体定为黑色了。。。。囧,谢谢你帮忙
但是这个程序原本我是希望它从26个大写字母里选13个,小写字母了选15个,数字里选7个,让后再从这35个中间随机一个让你猜,而且现在这个程序弄出来的数字好像根本不随机啊,从1猜到9算一遍,然后开始让你猜大写字母。。。。。你知道有什么办法改吗 --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
namespace ConsoleApplication6
{
public static class Program {
static void Main(string[] args)
{
StartGuess();
}
private static void StartGuess() {
Func<char, string> hint = new Func<char, string>(chr => string.Concat("当前是一个", 'A' < chr && chr < 'Z' ? "大写字母" : ('a' < chr && chr < 'z' ? "小写字母" : "数字")));
Func<char, Action, bool> guess = new Func<char, Action, bool>((chr, action) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(hint(chr));
Console.ForegroundColor = ConsoleColor.Black;
int innerguesscount = 0;
bool stop = false;
while (!stop)
{
Console.Write("当前已猜{0}次,请输入你猜的数:", innerguesscount);
try
{
string x = Console.ReadLine();
if (x.ToLower() == "quit") { action(); return true; };
char c = '-';
if (!char.TryParse(x, out c) || '0' > c || c > 'z' || Char.IsWhiteSpace(c)) throw new Exception();
Console.Write(c);
Console.ForegroundColor = ConsoleColor.Red;
if (c > chr) Console.WriteLine("\r\n提示:输入的字符太大");
else if (c < chr) Console.WriteLine("\r\n输入的字符太小");
else
{
Console.WriteLine("\r\n恭喜你,猜对了");
return true;
}
Console.ForegroundColor = ConsoleColor.Black;
innerguesscount++;
}
catch (Exception e)
{
Console.WriteLine("\r\n警告:输入的不是字母,也不是数字,重新输入吧");
}
}
return false;
});
var array = Enumerable.Concat(Enumerable.Concat(
RandomChoice(Enumerable.Range((byte)'A', 26).Select(n => (char)n).ToArray(), 13),
RandomChoice(Enumerable.Range((byte)'a', 26).Select(n => (char)n).ToArray(), 15)),
RandomChoice(Enumerable.Range((byte)'0', 10).Select(n => (char)n).ToArray(), 7)).OrderBy(c => c).ToArray();
Console.WriteLine("猜字符(输入quit退出游戏)\r\n");
int index = 0;
while (index < array.Length)
{
Console.WriteLine();
guess(array[index], () => { index = int.MaxValue - 1; Console.WriteLine("\r\n游戏结束"); });
index++;
}
}
private static List<T> RandomChoice<T>(T[] source/*Array类*/, int needCount)
{
List<T> result = new List<T>();//集合类
if (source == null || source.Length == 0) return result;
int length = source.Length;
if (needCount == 0) needCount = length;
int[] indexs = new int[length];
for (int i = 0; i < length; i++) indexs[i] = i;
const int MAXCOUNT = 1000;
Random rand = new Random(DateTime.Now.Millisecond);
int count = 0;
while (MAXCOUNT > count)
{
int first = rand.Next(0, length);
int second = rand.Next(0, length);
if (first == second) continue;
int tmp = indexs[first];
indexs[first] = indexs[second];
indexs[second] = tmp;
count++;
}
for (int i = 0; i < needCount; i++) result.Add(source[indexs[i]]);
return result;
}
}
}
谢谢你的答案啊,虽然这个和我们要求的不太一样,但还是谢谢你
补充:.NET技术 , C#