Java 如何编写将布尔值转换为“是”或“否”的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2216527/
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 write a method that translates a boolean into "yes" or "no"
提问by Tim
I'm new to java and I"m need to write a method that translates a boolean true or false into a string "yes" or "no". I'm kinda lost.
我是 Java 新手,我需要编写一个方法,将布尔值 true 或 false 转换为字符串“是”或“否”。我有点迷茫。
public class Book
{
private String title;
private String author;
private String isbn;
private int pages;
private boolean pback;
private double price;
/**
* Constructor for objects of class Book
*/
public Book(String bookTitle, String bookAuthor, String bookCode, int bookPages, boolean paperback, double bookRetail)
{
// initialise instance variables
title = bookTitle;
author = bookAuthor;
isbn = bookCode;
pages = bookPages;
pback = paperback;
price = bookRetail;
}
public String translate(boolean trueorFalse)
{
if(pback = true)
{
??????;
}
else(pback = false)
{
???????;
}
}
回答by Robert Munteanu
if(pback == true)
{
return "yes";
} else {
return "no";
}
A couple of things to note:
有几点需要注意:
- equality is tested using
==
, so you should writeif ( a == b )
, notif ( a = b )
; - returning a value from a method is done using the keyword
return
followed by the value; - else does not take a supplementary argument, unless you want to say
else if
which then takes an expression similar toif
, e.g.else if ( a ==b )
.
- 使用 测试相等性
==
,因此您应该编写if ( a == b )
,而不是if ( a = b )
; - 从方法返回值是使用关键字
return
后跟值来完成的; - else 不采用补充参数,除非您想说
else if
which then 采用类似于 的表达式if
,例如else if ( a ==b )
。
回答by tangens
boolean myBoolean = true;
String result = myBoolean ? "yes" : "no";
回答by MatrixFrog
if (pback) {
return "yes";
}
else {
return "no";
}
I feel like I'm missing something.
我觉得我错过了一些东西。
回答by Jon Skeet
The conditional operatoris your friend:
该条件运算符是你的朋友:
public static String translate(boolean trueOrFalse) {
return trueOrFalse ? "yes" : "no";
}
In general, if you find yourself writing:
一般来说,如果你发现自己在写:
SomeType x;
if (someCondition) {
x = someExpression;
} else {
x = someOtherExpression;
}
it's generally nicer to use:
通常使用更好:
SomeType x = someCondition ? someExpression : someOtherExpression;
The conditional operator makes sure that only one of someExpression
or someOtherExpression
is evaluated, so you can use method calls etc, confident that they won't be executed inappropriately.
条件运算符确保只计算someExpression
或之一someOtherExpression
,因此您可以使用方法调用等,确信它们不会被不恰当地执行。
Of course there are times when this gets too complicated - you need to judge the readability of each form for yourself.
当然,有时这会变得过于复杂 - 您需要自己判断每个表单的可读性。
回答by dma_k
The above advices should do the job, but I would recommend you to use:
上述建议应该可以完成这项工作,但我建议您使用:
public String translate(boolean trueOrFalse)
{
return String.valueOf(trueOrFalse);
}
because later you can easily convert that back:
因为稍后您可以轻松地将其转换回来:
public boolean translateBack(String translation)
{
return Boolean.parseBoolean(translation);
}
but the translation string will be "true" of "false" :)
但翻译字符串将是“假”的“真”:)
回答by fastcodejava
String yesNo(boolean b) {
String[] s = {"yes", "no"};
return b ? s[0] : s[1];
}
EDITED with correct return
编辑并正确返回
回答by CheesePls
First, the parameter for your translate method is never used. You should fix that.
首先,从不使用 translate 方法的参数。你应该解决这个问题。
Second, do you need to use the String values "Yes" and "No" for conditionals? If not and you can just use the boolean version(and I see no reason that you couldn't) I suggest leaving the boolean.
其次,您是否需要将字符串值“是”和“否”用于条件?如果没有,你可以只使用布尔版本(我认为你没有理由不能)我建议离开布尔版本。
You can then translate that boolean into "Yes" or "No" when it is outputted using something like the following code:
然后,当使用以下代码输出时,您可以将该布尔值转换为“是”或“否”:
//say your boolean variable is called gotIt
if(gotIt == true) //you can also just say if(gotIt) here
{
//here you place the string where it needs to be, either output it or place it into a variable
System.out.println("Yes");
}
else
{
//same as above but for false
System.out.println("No");
}
}
The fact is it is much easier to use conditionals with boolean values as opposed to testing 2 strings for equivalence.
事实上,与测试 2 个字符串的等价性相比,使用布尔值的条件要容易得多。
回答by Tomasz Godziński
There is a project from Apache Group called Apache Commons Langfor working with common Java classes like Boolean
. Its BooleanUtils
class has some nice methods to work with:
Apache Group 有一个名为Apache Commons Lang的项目,用于处理常见的 Java 类,如Boolean
. 它的BooleanUtils
类有一些很好的方法可以使用:
toStringOnOff(boolean bool) - converts a boolean to a String returning 'on' or 'off'
toStringOnOff(Boolean bool) - converts a Boolean to a String returning 'on', 'off' or null
toStringTrueFalse(boolean bool) - converts a boolean to a String returning 'true' or 'false'
toStringTrueFalse(Boolean bool) - converts a Boolean to a String returning 'true', 'false' or null
toStringYesNo(boolean bool) - converts a boolean to a String returning 'yes' or 'no'
toStringYesNo(Boolean bool) - converts a Boolean to a String returning 'yes', 'no' or null
In your example you should work with the toStringYesNo
method.
在您的示例中,您应该使用该toStringYesNo
方法。
boolean myBoolean = false;
String result = BooleanUtils.toStringYesNo(myBoolean);
System.out.println(result);
This will print
这将打印
no
To add the library to your project just add it to your Mavenpom.xml
dependency:
要将库添加到您的项目中,只需将其添加到您的Mavenpom.xml
依赖项中:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>