如何比较android中的字符串与大于 [英] How can you compare strings in android with greater than

查看:924
本文介绍了如何比较android中的字符串与大于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以比较android中的字符串与大于或>。

I was wondering if there is a way to compare strings in android with greater than or >.

假设我有这个:

String numbers = number.getText().toString();
if (numbers.equals("9")){
output.setText("50");}

所以如果在EditText字段中输入9,输出TextView将显示50.

我有很多不同的数字,然后=不同的数字,但什么可以如果我想要10,11,12,13等等= 100?

so if you enter 9 in the number EditText field the output TextView will display 50.
I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?

有办法通过使用这样的东西吗?

Is there a way to do this by using something like this?

if (numbers.equals("9"++))

或者在Android中有一些通配符。

or is there some kind of wildcard in android like

if (numbers.equals("1"+"*"))



我知道如果我用零替换*将是10所以如果这是可能的,我可以为1,一个为2,一个为3,等等,这将仍然保存我这么多的代码。

如果这是不可能让我知道如果你有任何想法。

i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
If this is not possible let me know if you have any ideas.

感谢各位!

推荐答案

需要首先将String转换为数字。像这样:

You'll need to convert the String to a number first. Something like this:

int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
    output.setText("50");
}

如果String不能保证是一个有效的整数, NumberFormatException

If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.

这篇关于如何比较android中的字符串与大于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆