如果你们中的任何人一直在关注我的缓慢学习过程,您就会知道发生了什么。
我创建了一个如下所示的边框矩形:
+--------+
| |
| |
| |
+--------+
Are you ready to play hangman? yes/no:
当然,它更大,更矩形。
不幸的是,使用我现在的代码,以前在矩形下方的单词现在在中间,像这样:
+--------+
|abcdefghi... |
Are you ready . . .
| |
| |
+--------+
现在,方框足够大,可以将所有单词放入其中,但初始问题应位于字段下方。这一切都始于我将字母表调用到一个数组中,然后让它显示在特定坐标处。
这是我的代码:
namespace Hangman
{
class Program
{
protected static int firstColumn;
protected static int firstRow;
protected static void headerWindow(string border, int posX, int posY)
{
try
{
Console.SetCursorPosition(firstColumn + posX, firstRow + posY);
Console.Write(border);
}
catch (ArgumentOutOfRangeException error)
{
Console.Clear();
Console.Write(error.Message);
}
}
private static void printWord()
{
String[] myWordArrays = File.ReadAllLines("WordList.txt");
Random randomWord = new Random();
//int lineCount = File.ReadLines("WordList.txt").Count();
int activeWord = randomWord.Next(0, myWordArrays.Length);
string userSelection = "";
Console.WriteLine("Are you Ready to play Hangman? yes/no: ");
userSelection = Console.ReadLine();
if (userSelection == "yes")
{
foreach (char letter in myWordArrays[activeWord])
{
Console.Write("_ ");
}
Console.WriteLine("\n \nCan you guess what this " + myWordArrays[activeWord].Length + " letter word is?");
Console.ReadLine();
}
else if (userSelection == "no")
{
Console.WriteLine("I'm sorry you feel that way. Press Enter to Exit the program!");
Console.ReadLine();
}
}
//THIS IS THE CREATION OF THE RECTANGLE!!!
private static void headerFile()
{
Console.Clear();
firstColumn = Console.CursorLeft;
firstRow = Console.CursorTop;
int HEADER_HEIGHT = 6;
int columnNumber = Console.WindowWidth - 1;
var xcoord = 0;
var ycoord = 0;
for (int i = 0; i < columnNumber; i++)
{
headerWindow("-", i, 0);
headerWindow("-", i, HEADER_HEIGHT);
}
for (int i = 0; i < HEADER_HEIGHT; i++)
{
headerWindow("|", 0, i);
headerWindow("|", columnNumber, i);
}
headerWindow("+", xcoord = 0, ycoord = 0);
headerWindow("+", xcoord = columnNumber, ycoord = 0);
headerWindow("+", xcoord = 0, ycoord = 6);
headerWindow("+", xcoord = columnNumber, ycoord = 6);
}
//THIS IS THE CREATION OF THE LIST OF UNUSED CHARACTERS FOR THE GAME
private static void letterChoices()
{
string[] alphabetSelection = File.ReadAllLines("alphabet.txt");
for (int i = 0; i < alphabetSelection.Length; i++)
{
headerWindow(alphabetSelection[i] + " ", i + 1, 1);
Console.WriteLine("\n ");
}
//return;
}
//SHOULD I HAVE MORE HERE??
static void Main(string[] args)
{
headerFile();
letterChoices();
printWord();
}
}
}
如果没有得到答案,我将不胜感激,因为我真的需要自己弄明白,但是我已经将方法调用从 main 移到了 headerwindow(),我什至将其全部单独写了出来方法。啊!
请帮忙!
最佳答案
由于您没有要求直接回答:修复属于 headerWindow
方法。根据您的偏好,同样的修复也可以应用于 letterChoices
。
当 headFile()
方法完成时,光标在框的左下角闪烁(在“正确”的位置写入文本)
在 letterChoices
循环之后,您的光标刚好位于字母表下方,因为它要求 headerWindow
在某个位置写入。
应该在 headerWindow
中更改一些内容,以便它不会将光标保持在刚刚写入的位置。您已经使用了创建此修复程序所需的每个属性/方法,因此它不是 Console
上的一些神秘修复程序或隐藏方法。
关于c# - 为什么一种方法被塞进另一种方法中? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579835/