我正在制作一个计算器应用程序,用于在按钮点击事件中创建一个对象。此计算器使用将运行总计存储为属性的对象,然后将新文本框中的新数字添加到该属性。 不幸的是每次点击按钮都会产生一个新对象,所以我没有一个恒定的运行总数。我试过if (object == null)
但为了做到这一点,我必须将对象设置为null按钮事件因此它将始终为null。根据我的分配,必须在click事件中实例化对象。
public class Calculator
{
public double Total = 0.0; // Running total for calculator
public double Addition(double number)
{
Total += number;
return Total;
}
public double Subtraction(double number)
{
Total -= number;
return Total;
}
public double Multiplication(double number)
{
Total *= number;
return Total;
}
public double Division(double number)
{
if (number != 0) // If number does not equal 0 Calculate
{
Total /= number;
return Total;
}
Total = 0; // If it does set the answer to 0 to avoid errors
return Total;
}
public virtual void Clear()
{
Total = 0.0;
}
}
public class ScientificCalculator : Calculator
{
public double Memory { get; set; } = 0.0; // Running total for calculator
public double Log(double number)
{
return Math.Log(number);
}
public double Sin(double number)
{
return Math.Sin(number);
}
public double Cos(double number)
{
return Math.Cos(number);
}
public double Tan(double number)
{
return Math.Tan(number);
}
public override void Clear()
{
Memory = 0.0;
}
}
public partial class CalcQ3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
ScientificCalculator calc = null;
bool isNumber = double.TryParse(txtNewNumber.Text, out double number); // Testing if textbox value is numeric
if (isNumber) // if numeric add number to total
{
if (calc == null) // if an instance has not been created creat one
{
calc = new ScientificCalculator();
}
calc.Addition(number);
lblTotal.Text = (calc.Total).ToString();
}
else // Display error
{
txtNewNumber.Text = string.Empty;
txtNewNumber.Focus();
lblTotal.Text = "Please Enter A Numeric Value";
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
ScientificCalculator calc = new ScientificCalculator();
calc.Clear();
lblTotal.Text = (calc.Total).ToString();
}
}
答案 0 :(得分:0)
很抱歉这个混乱。您只需要将calc作为静态
请参阅以下代码
map $status $limit {
default '';
401 $binary_remote_addr;
}
limit_req_zone $limit zone=api:10m rate=5r/s;
location /api {
limit_req zone=api burst=5;
...
}
}