如何在 Java 中将字符转换为布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26059061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a char to a boolean in Java?
提问by user6527926
I would like to display on the screen a question with two options (Are you married: y/n). The user would have to press or "y" or "n". After that I would like to print on the screen "married" or "not-married".
我想在屏幕上显示一个有两个选项的问题(你结婚了吗:是/否)。用户必须按“y”或“n”。之后我想在屏幕上打印“已婚”或“未婚”。
I've thought in using a char to get the input thorough the Scanner class (System.in) and after cast this char to a boolean type in order to display two options with an if statement. The problem is that I don′t know how to cast this char to a boolean type. Does it make sense? Is there other way to do this example in a different way?
我想过使用字符来获取通过 Scanner 类 (System.in) 的输入,然后将此字符转换为布尔类型,以便使用 if 语句显示两个选项。问题是我不知道如何将这个字符转换为布尔类型。是否有意义?有没有其他方法可以用不同的方式来做这个例子?
Many thanks
非常感谢
回答by Ruchira Gayan Ranaweera
Yes, the way you trying to do is doesn't make scenes. You can do as following
是的,你试图做的不是制作场景。你可以做如下
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
if("y".equalsIgnoreCase(st)){
// married
}else if("n".equalsIgnoreCase(st)){
// not married
}
回答by hyde
Very simple:
很简单:
boolean isYes = (inputChar=='y' || inputChar=='Y');
You could also convert the char to upper/lower case so you would not need ||
. Depends on how many tests you have, if it's worth it.
您还可以将字符转换为大写/小写,这样您就不需要||
. 取决于你有多少测试,是否值得。
And those parenthesis are not really needed in this case, as ||
has a higher precedence than assignment, but I personally find it clearer when "condition" like that is enclosed in parenthesis.
在这种情况下,这些括号并不是真正需要的,因为||
它的优先级高于赋值,但我个人认为,当像这样的“条件”括在括号中时,它会更清楚。
回答by duffymo
I think an enum would be a good way to go. Encapsulate the rules inside your objects.
我认为枚举将是一个好方法。将规则封装在您的对象中。
Binding user responses to objects is a view responsibility. Is the input a dropdown or text box?
将用户响应绑定到对象是视图责任。输入是下拉列表还是文本框?
Marital status is an interesting one. There are more flavors than "yes/no": single, married, divorced, separated, Kardashian, etc.
婚姻状况很有趣。有比“是/否”更多的味道:单身、已婚、离婚、分居、卡戴珊等。
public class MaritalStatus {
MARRIED, NOT_MARRIED;
}
回答by fabian
You cannot simply cast char
to boolean
, but you can compare it to 'y'
and 'n'
您不能简单地强制转换char
为boolean
,但您可以将其与'y'
和进行比较'n'
public static boolean charToBool(char c) {
// c = Character.toLowerCase(c);
switch (c) {
case 'y':
return true;
case 'n':
return false;
default:
throw new IllegalArgumentException("Must be either 'y' or 'n'.");
}
}
回答by Edwin
You can't cast a character into a boolean value, because how should the program know that 'y' means true and 'n' means false? Use an if statement
您不能将字符转换为布尔值,因为程序如何知道 'y' 表示真而 'n' 表示假?使用 if 语句
bool married = false;
if(input.equalsIgnoreCase("y"))
married = true;
I bet a simple google search would have helped you with your homework.
我打赌一个简单的谷歌搜索会帮助你完成你的家庭作业。