java -change boolean to yes no(java - 将布尔值更改为是 否)
问题描述
我有一个布尔数组,稍后需要将它们显示给用户,而不是显示真或假,我想显示是或否.有人可以帮我解决这个问题吗?
I have a boolean array and need them displayed to the user later, as opposed to showing true or false I'd like to display yes or no. Could someone please help me on this?
public static boolean[] seen (boolean[] birds)
{ String quit = "100";
String ans = "";
while(!ans.equals(quit))
{ ans=JOptionPane.showInputDialog(null,"Which bird are you reporting?
1) Blue Tit
2) Blackbird
3) Robin
4) Wren
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 格式,有人可以帮忙吗?
I would like the arrays newbirds[i] displayed as yes or no format, could someone help?
推荐答案
这可用于循环数组并打印yes"或no":
This can be used to loop over the array and print "yes " or "no " :
boolean[] newbirds = {false, true, true};
StringBuilder sb = new StringBuilder();
for(boolean bird : newbirds){
sb.append(bird? "yes " : "no ");
}
System.out.println(sb.toString());
这篇关于java - 将布尔值更改为是 否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java - 将布尔值更改为是 否
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01