
float aa = float.valueOf(a.getText().toString()); if (float.isNaN(aa)) { aa = 0; }我正在尝试检查用户没有输入,如果没有输入只是将其设为零,听起来很简单,但它不适合我.它是我的第一个AndroID应用程序,当没有用户输入并且用户按下go按钮时它崩溃.有人可以帮忙吗?
谢谢.
解决方法:
I’m trying to check for no input from the user and if there’s no input just make it zero.
当没有输入或输入无效时,valueOf不返回NaN; it throws NumberFormatException.你可以在valueOf调用周围添加一个try / catch(因为你想要float,而不是float,你应该使用parsefloat来避免不必要的装箱和取消装箱的值):
float aa;try { aa = float.parsefloat(a.getText().toString());} catch (NumberFormatException nfe) { aa = 0;} 总结 以上是内存溢出为你收集整理的Java – if(Float.isNaN(x))全部内容,希望文章能够帮你解决Java – if(Float.isNaN(x))所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)