- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做家庭作业,在这项作业中我得到了入门代码。我们正在学习我以前做过的类(class),但似乎被这些类(class)淹没了。入门代码让我感到困惑,我个人宁愿自己开始作业。我对此有几个问题,我想知道是否有人可以帮助我解释它,以便我可以完成这项任务。
我最大的问题很简单。如果您有计算器,您会得到第一个数字,然后是第二个数字。所以如果用户输入 1 + 1 那么你基本上就有了你的数字。我们得到的代码没有 firstNumber 和 secondNumber。相反,如果我理解正确的话,它有 displayValue 和 currentValue(?)我不完全确定这些值是如何存储的,我相信它们已经存储了?
这真的是我想要找出的,然后我是否正确地编写了我的类(class)代码,因为我挂断了我输入的实际数字是如何存储在程序中的。
这是提供的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class frmCalculator : Form
{
public frmCalculator()
{
InitializeComponent();
}
// The following fields are used to store the value that's currently
// displayed by the calculator. displayString is a string value that's
// constructed as the user clicks numeric keys and the decimal and +/-
// key. The Convert.ToDecimal method is then used to convert this to a decimal
// field that's stored in displayValue.
private string displayString;
private decimal displayValue;
// The following bool fields are used to control numeric entry.
// newValue indicates whether the calculator is ready to receive a
// new numeric value. Once the user clicks a digit button, newValue is
// set to false. When the user clicks a button that "enters" the value,
// such as Add or Equals, newValue is set to true so the user can enter
// another value.
// decimalEntered is used to restrict the entry to a single decimal point.
// It is set to true whenever newValue is set to true, and it is set to
// false whenever the user clicks the decimal point button.
private bool newValue;
private bool decimalEntered;
private Calculator calc = new Calculator();
private void Form1_Load(object sender, System.EventArgs e)
{
displayValue = 0;
displayString = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
// This method handles the 0 through 9 keys, appending the digit clicked
// to the displayString field.
private void btnNumber_Click(object sender, System.EventArgs e)
{
if (newValue)
{
displayString = "";
newValue = false;
}
displayString += ((Button)sender).Tag.ToString();
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
}
// This method removes the last character from the displayString field.
private void btnBackSpace_Click(object sender, System.EventArgs e)
{
if (displayString.Length > 1)
{
displayString = displayString.Substring(0, displayString.Length - 1);
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
}
else
{
displayString = "";
displayValue = 0;
txtDisplay.Text = displayValue.ToString();
}
}
private void btnClear_Click(object sender, System.EventArgs e)
{
calc.Clear();
displayString = "";
displayValue = 0;
txtDisplay.Text = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
// This method appends a decimal point to the displayString field if the
// user has not already entered a decimal point.
private void btnDecimal_Click(object sender, System.EventArgs e)
{
if (newValue)
{
displayString = "0";
newValue = false;
}
if (!decimalEntered)
{
displayString += ".";
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
decimalEntered = true;
}
}
private void btnSign_Click(object sender, System.EventArgs e)
{
displayValue = -displayValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
calc.Add(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnSubtract_Click(object sender, System.EventArgs e)
{
calc.Subtract(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnMultiply_Click(object sender, System.EventArgs e)
{
calc.Multiply(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnDivide_Click(object sender, System.EventArgs e)
{
calc.Divide(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
//private void btnSqrt_Click(object sender, System.EventArgs e)
//{
// calc.SquareRoot(displayValue);
//displayValue = calc.CurrentValue;
//txtDisplay.Text = displayValue.ToString();
//}
//private void btnReciprocal_Click(object sender, System.EventArgs e)
//{
// try
// {
// calc.Reciprocal(displayValue);
// displayValue = calc.CurrentValue;
//txtDisplay.Text = displayValue.ToString();
//}
//catch (DivideByZeroException)
//{
// displayValue = 0;
// txtDisplay.Text = "Cannot divide by zero.";
//newValue = true;
//decimalEntered = false;
//}
}
private void btnEquals_Click(object sender, System.EventArgs e)
{
try
{
if (newValue)
calc.Equals();
else
calc.Equals(displayValue);
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
catch (DivideByZeroException)
{
displayValue = 0;
txtDisplay.Text = "Cannot divide by zero.";
newValue = true;
decimalEntered = false;
}
}
}
}
然后是我类的一个样本(为了节省空间和时间,我只会做我的一个操作数)
namespace Calculator
{
public class Calculator
{
public Decimal displayValue;
public Decimal currentValue;
public Decimal firstNumber;
public Decimal secondNumber;
public decimal Add(Decimal displayValue)
{
return displayValue + currentValue;
}
然后我假设因为没有 currentValue 并且它来 self 假设它是引用 displayValue 的类?虽然我不确定这是否会回到用户刚刚输入的内容,或者它是否会基本上重复? (如果这是有道理的……但我的意思是你可以看出我对这部分有多困惑)。
所以真的是的,我只是在找人来解释它,如果我理解正确并朝着正确的方向前进,请告诉我......否则我想我搞砸了......
提前致谢。
最佳答案
您必须将结果存储在 current Value
变量中,因为稍后会在代码中使用它。此外,计算器方法的使用表明它们不应返回任何值。
public void Add(Decimal displayValue)
{
currentValue += display Value;
}
这样做的原因是 btnAdd_Click
中的以下行:
displayValue = calc.CurrentValue;
它期望 CurrentValue
属性保存当前值。
关于C#计算器给定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919658/
我目前正在尝试使用 ParaView Calculator-Filter 将给定的笛卡尔坐标 (x,y,z) 转换为球坐标 (r, theta, phi),其中 theta 是极角,phi 是方位角。
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有这个问题,我想显示如果0/0,输出是:“不能将0除以自身”。如何调整我的代码以便可以显示该输出?如果是这样,我应该使用什么代码才能实现我的目标? 下面是我的代码: #include using
我正在尝试创建一个也支持负数的计算器,并最终创建一个 lisp 风格的树。 我像这样定义词法分析器规则: INT :'-'? [0-9]+ ; LBRACKET : '('; RBRACKET :
我正在开发一个基本的 JavaScript 计算器,我也希望能够开始计算负数。现在,如果我在输入数字之前单击“-”,“-”将不会显示,因此我只能从正数开始。有人可以告诉我如何将其包含在我的代码中吗?
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
这是我第一次尝试 Java。该项目是一个计算器,它需要一个数字、一个运算符(operator)信号(+、-、*、/)和另一个数字来创建方程并给出其最终值,然后询问用户是否要重新启动程序另一个方程或不是
所以我写了这个脚本;它有点像我找到并拼凑起来的计算器的大杂烩。 KeyListener 来自 Java - oracle - .com 网站。顺便说一句,我对此非常陌生,不知道我在做什么。 我正在尝试
我正在尝试创建一个也支持负数的计算器,并最终创建一个 lisp 风格的树。 我像这样定义词法分析器规则: INT :'-'? [0-9]+ ; LBRACKET : '('; RBRACKET :
我正在开发一个基本的 JavaScript 计算器,我也希望能够开始计算负数。现在,如果我在输入数字之前单击“-”,“-”将不会显示,因此我只能从正数开始。有人可以告诉我如何将其包含在我的代码中吗?
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我开始在java中创建一个计算器,我试图循环遍历字符串输入,如果输入中有任何整数,则将它们添加到ArrayList calcOperands中。在 parseInput() 方法中,我使用 charA
我正忙着制作计算器,但不知怎的,整数没有被使用,我不知道为什么。我尝试修复它,但我不知道该怎么做。我使用带有事件的按钮来计算答案,也许有问题。这是我的代码:顺便说一句,我使用 Eclipse
我的主类中有这段代码。我的问题是,GPA 是用总分除以类(class)来计算的。它没有给我完整的号码。 EX,如果总数为 14,类(class)为 4,则为 3.5,我的代码只给我 3.0。有谁知道为
我需要创建一个可以加、减、乘、除、绝对值和舍入的计算器。这是我到目前为止所拥有的 import java.util.Scanner; public class Calculator { pub
我是一名 Java Noob,正在研究 GUI 计算器,但我刚刚来到这里..我已经有了按钮,我需要绑定(bind)这些数字并存储在运算符 ( + - */) 之间的某个位置以显示在我的 JTextAr
这是我的作业。但是,我无法让结果发挥作用。我希望它打印出来为: > 2*7*6 2 * 7 ---- 14 * 6 ---- 84 等等。我希望无论我输入多少个数字,代码都能正常工作
这个问题已经有答案了: What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? (18 个回答) 已关闭 6 年
大家好,感谢您帮助我。 我用 C# 制作了这个计算器,但遇到了一个问题。 当我添加像 5+5+5 这样的东西时,它给了我正确的结果,但是当我想减去两个以上的数字并且还想除或乘以两个以上的数字时,我没有
我一直在开发计算器作为自己的学习项目。它工作正常,只是我无法弄清楚如何阻止人们添加应用程序破坏输入,例如 1++-*/4。我尝试了各种方法,例如将当前显示拆分为一个数组,并将其与具有所有运算符的另一个
我是一名优秀的程序员,十分优秀!