0
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);
}
}

I need my program to print the character names when they are at the tavern instead of true/false when they are at the tavern. I would greatly appreciate any help. I'm a beginner to java so the switch statement hasn't been explained to me. I think we are going back to clean up this code later in my CSC 145 class.

4
  • What language are you trying to write here? The question says Javascript, but the code is Java. Commented Feb 26, 2017 at 18:53
  • Perhaps you want to use a HashMap<String, Boolean>, a class that maps each String with a boolean value. Commented Feb 26, 2017 at 18:54
  • if (condition) { variable = true; } else { variable = false; } can be more easily written as variable = condition;. Commented Feb 26, 2017 at 19:19
  • Also note you change between || and | in the while guard. There is no behavioural difference in this case, but in general there might be. Typically, || is the right one to use. Commented Feb 26, 2017 at 19:21

1 Answer 1

1

Try this:

  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();

But a HashMap would have been better.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to everyone for the help. I appreciate all the suggestions. I really was kinda stumped on this. I will make the appropriate corrections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.