仅检测计算器应用程序文本输入框中的最后两位数字
我对此还很陌生,但我正在尝试制作一个计算器供自己在工作中使用;类似于电阻计算器。
我希望根据输入到编辑文本框中的最后两位数字来更改文本视图和图像视图。例如,如果我要输入“9,000,011”,我希望显示与“11”相对应的特定颜色的图像和文本,以及与 1,000 对应的相同颜色和文本,011。 12、13 等也不同。这样,无论我输入什么数字,它都只会查看最后两位数字。有谁知道如何做到这一点,或者也许可以指出我正确的方向?
我就是这样
private void calculate() {
number = Double.parseDouble(inputnumber.getText().toString());
ImageView iv = (ImageView) this.findViewById(R.id.pairimage);
if (number == 6000001) {
iv.setImageResource(R.drawable.white);
txtnumber.setText("White");
} else if (number == 6000002) {
iv.setImageResource(R.drawable.red);
txtnumber.setText("Red");
}
//*and so on, all the way up to 99*
}
I'm still pretty new to this, but I'm trying to make a calculator for myself to use at work; similar to a resistor calculator.
I am hope to change a textview and imageview according to the last two digits entered into a edittext box. For example, if I were to type in "9,000,011", I would want to display a certain color of image and text that corresponds with "11" and the same color and text for say 1,000,011. Also different for 12, 13, and so on. this way No matter what number I type it only looks at the last two digits. Does anyone know the way to do this or maybe can just point me in the right direction?
here is how I'm
private void calculate() {
number = Double.parseDouble(inputnumber.getText().toString());
ImageView iv = (ImageView) this.findViewById(R.id.pairimage);
if (number == 6000001) {
iv.setImageResource(R.drawable.white);
txtnumber.setText("White");
} else if (number == 6000002) {
iv.setImageResource(R.drawable.red);
txtnumber.setText("Red");
}
//*and so on, all the way up to 99*
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只想要最后两个,那么我会使用
switch
块:等等。
If you want just the last two, then I would use
and then a
switch
block:etc.
也许将数字转换为字符串,然后查看例如
然后您可以使用 if 语句来读取最后两个数字。或者转换回 int
(Integer.valueOf(lastTwoDigits);
并使用 switch 语句。或者将值 0-99 放入 Map 中,并使用 Command 对象或其他内容作为执行的值。显然,您需要对用户输入进行一些验证。
Maybe convert the number to a String and then look at that e.g.
Then you can have your if statements to read the lastTwoDigits. Or convert back to int
(Integer.valueOf(lastTwoDigits);
and use a switch statement. Or put the values 0-99 into a Map and have a Command object or something as the value which gets executed.Obviously you'll need some validation here on the user input.
你有几个选择。最简单的方法是使用模运算符,例如:
不过,要使此功能起作用,“数字”必须是整数。在您的示例中它是一个整数,因此可能对您有用;否则你可以尝试一些数学技巧,例如:
You have a couple of options. The easiest one is to use the modulus operator like:
For this to work, though, "number" will have to be an integer. It's an integer in your examples, so that may work for you; otherwise you can try some math tricks like:
采用子字符串方法。我发现一些数字组合在java中不能很好地乘法/除法。
Go with the sub-string approach. I've found that some number combinations don't multiply/divide well in java.