gpt4 book ai didi

Java - 如果 (Float.isNaN(x))

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:45 27 4
gpt4 key购买 nike

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不返回NaNit throws NumberFormatException .您可以在 valueOf 调用周围添加一个 try/catch(因为您需要 float,而不是 Float ,您应该使用 parseFloat 来避免对值进行不必要的装箱和拆箱):

float aa;

try {
aa = Float.parseFloat(a.getText().toString());
} catch (NumberFormatException nfe) {
aa = 0;
}

关于Java - 如果 (Float.isNaN(x)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com