仅检测计算器应用程序文本输入框中的最后两位数字

发布于 2024-11-05 07:50:55 字数 732 浏览 0 评论 0原文

我对此还很陌生,但我正在尝试制作一个计算器供自己在工作中使用;类似于电阻计算器。

我希望根据输入到编辑文本框中的最后两位数字来更改文本视图和图像视图。例如,如果我要输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尘世孤行 2024-11-12 07:50:55

如果您只想要最后两个,那么我会使用

String wholeNumber = inputnumber.getText().toString();
int n = Integer.valueOf(wholeNumber.subString(wholeNumber.length()-2, wholeNumber.length()-1);

switch 块:

switch(n) {
case 1:
  iv.setImageResource(R.drawable.white);
  txtnumber.setText("White");
  break;
case 2:
  iv.setImageResource(R.drawable.red);
  txtnumber.setText("Red");
  break;
}

等等。

If you want just the last two, then I would use

String wholeNumber = inputnumber.getText().toString();
int n = Integer.valueOf(wholeNumber.subString(wholeNumber.length()-2, wholeNumber.length()-1);

and then a switch block:

switch(n) {
case 1:
  iv.setImageResource(R.drawable.white);
  txtnumber.setText("White");
  break;
case 2:
  iv.setImageResource(R.drawable.red);
  txtnumber.setText("Red");
  break;
}

etc.

岁月静好 2024-11-12 07:50:55

也许将数字转换为字符串,然后查看例如

    String theNumber = String.valueOf(number);
    String lastTwoDigits =
       theNumber.substring(theNumber.length() -2, theNumber.length());

然后您可以使用 if 语句来读取最后两个数字。或者转换回 int (Integer.valueOf(lastTwoDigits); 并使用 switch 语句。或者将值 0-99 放入 Map 中,并使用 Command 对象或其他内容作为执行的值。

显然,您需要对用户输入进行一些验证。

Maybe convert the number to a String and then look at that e.g.

    String theNumber = String.valueOf(number);
    String lastTwoDigits =
       theNumber.substring(theNumber.length() -2, theNumber.length());

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.

梦幻的味道 2024-11-12 07:50:55

你有几个选择。最简单的方法是使用模运算符,例如:

number = number % 100;
switch (number) {
  case 1:
    // do stuff;
    break;
  case 2:
    // do stuff;
    break;
}

不过,要使此功能起作用,“数字”必须是整数。在您的示例中它是一个整数,因此可能对您有用;否则你可以尝试一些数学技巧,例如:

int number2 = (int)(number * 100) % 100;

You have a couple of options. The easiest one is to use the modulus operator like:

number = number % 100;
switch (number) {
  case 1:
    // do stuff;
    break;
  case 2:
    // do stuff;
    break;
}

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:

int number2 = (int)(number * 100) % 100;
慕巷 2024-11-12 07:50:55

采用子字符串方法。我发现一些数字组合在java中不能很好地乘法/除法。

Go with the sub-string approach. I've found that some number combinations don't multiply/divide well in java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文