【发布时间】:2014-12-09 03:06:59
【问题描述】:
我不断收到以下未分配变量的错误,即TotalAmount = TotalQuantity * UnitPrice。请指教。
private void button1_Click(object sender, EventArgs e)
{
string client;
string date;
string telNumber;
const decimal TaxRate = 0.43M;
uint Quantity, Amount, Total;
decimal TotalOrder, TaxAmount, SalesTotal, TotalQuantity;
decimal UnitPrice, AmountTended, Difference, TotalAmount;
Console.SetCursorPosition(10, 5);
Console.ForegroundColor = ConsoleColor.Yellow;
client = Console.ReadLine();
Console.SetCursorPosition(10, 7);
Console.ForegroundColor = ConsoleColor.Yellow;
client = Console.ReadLine();
Console.SetCursorPosition(58, 5);
Console.ForegroundColor = ConsoleColor.Yellow;
date = Console.ReadLine();
Console.SetCursorPosition(67, 7);
Console.ForegroundColor = ConsoleColor.Yellow;
telNumber = Console.ReadLine();
//TotalQuantity
bool test = false;
do
{
try
{
Console.SetCursorPosition(2, 12);
Console.Write(" ");
Console.SetCursorPosition(2, 12);
TotalQuantity = Convert.ToDecimal(Console.ReadLine());
test = false;
}
catch
{
test = true;
}
} while (test);
//Item Description
Console.SetCursorPosition(18, 12);
Console.ForegroundColor = ConsoleColor.Yellow;
telNumber = Console.ReadLine();
//Unit Price
bool test2 = false;
do
{
try
{
Console.SetCursorPosition(47, 12);
Console.Write(" ");
Console.SetCursorPosition(47, 12);
UnitPrice = Convert.ToDecimal(Console.ReadLine());
test2 = false;
}
catch
{
test2 = true;
}
} while (test2);
//Computations
//TotalAmount
**TotalAmount = TotalQuantity*UnitPrice;
**
Console.SetCursorPosition(65, 12);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("P ");
Console.WriteLine(TotalQuantity*UnitPrice);
Console.ReadLine();
}
【问题讨论】:
-
您是否在 TotalAmount 周围使用 ** 和 ** 进行编译?
-
我不同意这是重复的。是的,这是相同的错误消息。但是另一个问题涉及一个 真正 未分配的变量,而这里只是编译器无法识别分配。此外,另一个问题中唯一合适的答案是初始化变量,而这里这些变量实际上不需要在实际分配之前进行预初始化。将此问题视为另一个问题的重复是误导性的。
-
我在发布这个之前确实检查了答案和问题,感谢格兰特先生,我得到了答案,也谢谢彼得爵士
标签: c#