java 你怎么能比较android中的字符串大于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4092161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:44:12  来源:igfitidea点击:

How can you compare strings in android with greater than

javaandroidcompare

提问by Daniel

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

我想知道是否有办法将 android 中的字符串与大于或 > 进行比较。

Lets say I have this:

可以说我有这个:

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

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?

因此,如果您在数字 EditText 字段中输入 9,则输出 TextView 将显示 50。
我有很多不同的数字,它们将 = 不同的数字,但是如果我想要 10、11、12、13 等,我该怎么办 = 100?

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

有没有办法通过使用这样的东西来做到这一点?

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

or is there some kind of wildcard in android like

或者在android中是否有某种通配符

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

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.

我知道如果我用零替换 * 它将是 10,所以如果可能的话,我可以制作一个为 1,一个为 2,一个为 3,等等,这仍然可以为我节省很多代码。
如果这是不可能的,请告诉我您是否有任何想法。

Thanks guys!

多谢你们!

回答by Dan Dyer

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");
}

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

如果不能保证 String 是有效整数,您也必须处理NumberFormatException

回答by Falmarri

Is there a reason you can't use

有什么不能使用的原因吗

Integer.valueOf("9");