- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有关于使用 Boolean.valueOf(String)
的问题。在我的代码中,用户将通过输入 true
或 false
来回答问题。然后应该将字符串转换为 boolean 值
。
public String setCorrespondingAuthor ()
{
String respons = "true";
do
{
System.out.print("Is s/he the corresponding author? (true/false)");
respons = sc.next();
sc.nextLine();
} while (!respons.equalsIgnoreCase("true")
&& !respons.equalsIgnoreCase("false"));
boolean flag = Boolean.valueOf(respons);
if (!flag)
{
return "not the corresponding author";
}
return "the corresponding author";
}
目前,一切正常。问题在于,在输出中,它在处理之前提示问题两次。
最佳答案
问题是您从用户输入中读取了两次:sc.next()
和 sc.nextLine()
。您应该只读取一次并将该值存储在您的 respons
变量中。
您还应该考虑对字符串文字(例如 "true"
、 "false"
)调用 equalsIgnoreCase
,而不是对变量调用 equalsIgnoreCase
,因为变量可能为 null
,从而导致 NullPointerException
。
String respons = "true";
do
{
System.out.print("Is s/he the corresponding author? (true/false)");
respons = sc.nextLine();
} while (!"true".equalsIgnoreCase(respons)
&& !"false".equalsIgnoreCase(response));
return Boolean.valueOf(respons) ? "the corresponding author" : "not the corresponding author";
关于java - Boolean.valueOf() 的良好用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26368120/
嗨,我正在考虑开发一种文件传输程序,想知道我是否想要尽可能好的加密,我应该使用什么? 我会用 C# 开发它,所以我可以访问 .net 库 :P在我的 usb 上有一个证书来访问服务器是没有问题的,如果
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
我已经能够使用 RTCPeerConnection.getStats() API 获得 WebRTC 音频调用的各种统计信息(抖动、RTT、丢包等)。 我需要将整体通话质量评为优秀、良好、一般或差。
基本问题: 如果我正在讲述/修改数据,我应该通过索引硬编码索引访问文件的元素,即 targetFile.getElement(5);通过硬编码标识符(内部翻译成索引),即 target.getElem
在 Linux 上,我想知道要调用什么“C”API 来获取每个 CPU 的统计信息。 我知道并且可以从我的应用程序中读取 /proc/loadavg,但这是系统范围的负载平均值,而不是每个 CPU 的
在客户端浏览器中使用 fetch api,GET 或 POST 没有问题,但 fetch 和 DELETE 有问题。它似乎将 DELETE 请求方法更改为 OPTIONS。 大多数研究表明是一个cor
我是一名优秀的程序员,十分优秀!