public class LordofTheRings {
public static void main(String[] args){
boolean Gimli=false;
boolean Boromir=false;
boolean Aragorn=false;
boolean Sam=false;
boolean Frodo=false;
boolean Legolas=false;
boolean Merry=false;
boolean Pippin=false;
boolean Gandalf=false;
int night1=1;
while (!Gimli||!Boromir||!Aragorn||!Sam||!Frodo|!Legolas|!Merry|!Pippin|!Gandalf){
Gimli=true;
if (night1 % 2==0)
Boromir=true;
else
{
Boromir=false;
}
if (night1 % 3==0)
Aragorn=true;
else
{
Aragorn=false;
}
if (night1 % 4==0)
Sam=true;
else
{
Sam=false;
}
if (night1 % 5==0)
Frodo=true;
else
{
Frodo=false;
}
if (night1 % 6==0)
Legolas=true;
else
{
Legolas=false;
}
if (night1 % 7==0)
Merry=true;
else
{
Merry=false;
}
if (night1 % 8==0)
Pippin=true;
else
{
Pippin=false;
}
if (night1 % 9==0)
Gandalf=true;
else
{
Gandalf=false;
}
System.out.println("Night "+night1);
System.out.println("=========================");
System.out.println("LOTR characters at the tavern: " + Gimli + Boromir + Aragorn + Sam + Legolas + Merry + Pippin + Gandalf);
}
}
当他们在小酒馆时,我需要我的程序打印字符名称,而不是真/假。我非常感谢任何帮助。我是java的初学者,所以我没有向我解释switch语句。我想我们将在稍后的CSC 145课程中回过头来清理这段代码。
答案 0 :(得分:1)
试试这个:
System.out.print("LOTR characters at the tavern: ");
System.out.print(Gimli ? "Gimli " : "");
System.out.print(Boromir ? "Boromir " : "");
System.out.print(Aragorn ? "Aragorn " : "");
System.out.print(Sam ? "Sam " : "");
System.out.print(Legolas ? "Legolas " : "");
System.out.print(Merry ? "Merry " : "");
System.out.print(Pippin ? "Pippin " : "");
System.out.print(Gandalf ? "Gandalf" : "");
System.out.println();
但是HashMap会更好。