How can you compare strings in android with greater than(如何比较 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.
谢谢大家!
推荐答案
您需要先将字符串转换为数字.像这样的:
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");
}
如果不能保证字符串是一个有效的整数,你也必须处理 NumberFormatException.
If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.
这篇关于如何比较 android 中的字符串与大于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何比较 android 中的字符串与大于
				
        
 
            
        基础教程推荐
- 从 python 访问 JVM 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				