java - 将布尔值更改为是 否
java -change boolean to yes no
我有一个布尔数组,稍后需要将它们显示给用户,而不是显示 true 或 false 我想显示 yes 或 no。有人可以帮我解决这个问题吗?
public static boolean[] seen (boolean[] birds)
{ String quit = "100";
String ans = "";
while(!ans.equals(quit))
{ ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n 1) Blue Tit\n 2) Blackbird\n 3) Robin\n 4) Wren\n 5) Greenfinch");
if (ans.equals("1"))
{ birds[0]=true;
}
else if (ans.equals("2"))
{ birds[1]=true;
}
else if (ans.equals("3"))
{ birds[2]=true;
}
else if (ans.equals("4"))
{ birds[3]=true;
}
else if (ans.equals("5"))
{ birds[4]=true;
}
}
return birds;
}
public static void display(boolean[] newbirds)
{ JOptionPane.showMessageDialog(null,"newbirds[0]+" "+newbirds[1]+" "+newbirds[2]+" "+newbirds[3]+" "+ newbirds[4]+"");
}
}
我希望数组 newbirds[i] 显示为 yes 或 no 格式,有人可以帮忙吗?
// Use ternary operator :
String yesOrNo = condition?result1:result2
// so if condition is true the returned value will be result1 else it will be result2.
JOptionPane.showMessageDialog(null, newbirds[0]?"Yes":"No"+" "+newbirds[1]?"Yes":"No"+" "+newbirds[2]?"Yes":"No"+" "+newbirds[3]?"Yes":"No"+" "+ newbirds[4]?"Yes":"No");
// or you can make a method to get the value.
String yesOrNo(boolean b){
return b?"Yes":"No";
}
//use it like
JOptionPane.showMessageDialog(null,yesOrNo(newbirds[index]));
您可以像这样为其定义一个方法
public String booleanToString(boolean b) {
return b ? "yes" : "no";
}
我上面用的叫三元运算符。
它是这样工作的:
boolean (expression) ? actionIfTrue : actionIfFalse
第一部分我觉得很简单。它只是任何表达式或变量或任何是或评估为布尔值的东西。例如 a == b
或 true
.
第二部分是actionIfTrue
只有当表达式是true
时才会被调用。
第三部分是 actionIfFalse
它仅在表达式为 false
.
时调用
它的工作原理类似于缩短的 if
。上面的 if
语句看起来像这样:
if (b)
return "yes";
else
return "no";
这样使用:
boolean a = false;
boolean b = !a;
// etc
someMethod( booleanToString(a) + " xyz " + booleanToString(b) );
这可用于遍历数组并打印 "yes " 或 "no " :
boolean[] newbirds = {false, true, true};
StringBuilder sb = new StringBuilder();
for(boolean bird : newbirds){
sb.append(bird? "yes " : "no ");
}
System.out.println(sb.toString());
您可以使用 ApacheCommons
从字符串到布尔值
BooleanUtils.toBooleanObject(string)
从布尔值到字符串
BooleanUtils.toString(boolVal,"Yes","No")
我有一个布尔数组,稍后需要将它们显示给用户,而不是显示 true 或 false 我想显示 yes 或 no。有人可以帮我解决这个问题吗?
public static boolean[] seen (boolean[] birds)
{ String quit = "100";
String ans = "";
while(!ans.equals(quit))
{ ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n 1) Blue Tit\n 2) Blackbird\n 3) Robin\n 4) Wren\n 5) Greenfinch");
if (ans.equals("1"))
{ birds[0]=true;
}
else if (ans.equals("2"))
{ birds[1]=true;
}
else if (ans.equals("3"))
{ birds[2]=true;
}
else if (ans.equals("4"))
{ birds[3]=true;
}
else if (ans.equals("5"))
{ birds[4]=true;
}
}
return birds;
}
public static void display(boolean[] newbirds)
{ JOptionPane.showMessageDialog(null,"newbirds[0]+" "+newbirds[1]+" "+newbirds[2]+" "+newbirds[3]+" "+ newbirds[4]+"");
}
}
我希望数组 newbirds[i] 显示为 yes 或 no 格式,有人可以帮忙吗?
// Use ternary operator :
String yesOrNo = condition?result1:result2
// so if condition is true the returned value will be result1 else it will be result2.
JOptionPane.showMessageDialog(null, newbirds[0]?"Yes":"No"+" "+newbirds[1]?"Yes":"No"+" "+newbirds[2]?"Yes":"No"+" "+newbirds[3]?"Yes":"No"+" "+ newbirds[4]?"Yes":"No");
// or you can make a method to get the value.
String yesOrNo(boolean b){
return b?"Yes":"No";
}
//use it like
JOptionPane.showMessageDialog(null,yesOrNo(newbirds[index]));
您可以像这样为其定义一个方法
public String booleanToString(boolean b) {
return b ? "yes" : "no";
}
我上面用的叫三元运算符。 它是这样工作的:
boolean (expression) ? actionIfTrue : actionIfFalse
第一部分我觉得很简单。它只是任何表达式或变量或任何是或评估为布尔值的东西。例如 a == b
或 true
.
第二部分是actionIfTrue
只有当表达式是true
时才会被调用。
第三部分是 actionIfFalse
它仅在表达式为 false
.
它的工作原理类似于缩短的 if
。上面的 if
语句看起来像这样:
if (b)
return "yes";
else
return "no";
这样使用:
boolean a = false;
boolean b = !a;
// etc
someMethod( booleanToString(a) + " xyz " + booleanToString(b) );
这可用于遍历数组并打印 "yes " 或 "no " :
boolean[] newbirds = {false, true, true};
StringBuilder sb = new StringBuilder();
for(boolean bird : newbirds){
sb.append(bird? "yes " : "no ");
}
System.out.println(sb.toString());
您可以使用 ApacheCommons
从字符串到布尔值
BooleanUtils.toBooleanObject(string)
从布尔值到字符串
BooleanUtils.toString(boolVal,"Yes","No")