【发布时间】:2013-01-26 09:15:15
【问题描述】:
这个程序在任何给定的坐标处打印出一个字符串。它应该包含一些前景色和背景色。我有一个错误提示警告 1 The field 'ConsoleApplication1.ConsoleText.color' is never used 这是我的代码:
class ColoredText
{
public int x = 10;
public int y = 20; // Coordinates
public string Text = "Hello!";
ConsoleColor color = ConsoleColor.Blue;
public ColoredText(int x, int y, string Text)
{
Console.ForegroundColor = color;
Console.BackgroundColor = color;
Console.SetCursorPosition(20, 0);
Console.Clear();
Console.ResetColor();
}
public virtual void Draw()
{
if (x >= 80 || y >= 49 || x < 0 || y < 0)
{
Console.WriteLine("Värdet är inte giltigt");
}
else
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(Text);
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
}
}
}
关于错误有什么问题的任何想法?
【问题讨论】:
-
警告告诉你一切,你没有使用
color,你在代码中的任何地方定义了ConsoleColor color = ConsoleColor.Blue; -
但是如果你看一下我的绘制方法,我已经将前景色和背景色分配给 ConsoleColor.Blue,这是颜色的值
-
是的,您可以使用
color字段,例如:Console.ForegroundColor = color;或者您可以从代码中删除您的字段color