gpt4 book ai didi

java - 临时字符串 boolean 值?

转载 作者:行者123 更新时间:2023-12-01 14:41:43 25 4
gpt4 key购买 nike

我正在尝试为我正在创建的项目创建一种设置系统,但是我遇到了一些问题。我正在尝试使用字符串(考虑到这些设置稍后必须保存到文本文件中)作为临时 boolean 值。所以,我需要能够在 true 和 false 之间切换这个字符串。我假设以下方法可行,但由于某种原因它总是设置为 true 。据我所知,这是代码中唯一设置变量的地方。

我不知道为什么下面的代码不起作用,我已经进行了彻底的搜索,但没有结果。

if("true".equals(settings[0])) 
settings[0] = "false"; //This is *always* skipped
else if("false".equals(settings[0]))
settings[0] = "true";`

所以我才来到这里。

最佳答案

将字符串转换为 boolean 值并取反结果。示例

String[] settings = {"tRue"};
boolean settingBool = !Boolean.parseBoolean(settings[0]);
System.out.println(settingBool);

输出:

false

关于java - 临时字符串 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15888506/

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